Advertisement
HaniiPuppy

thread safety workaround

Jul 2nd, 2014
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1.     Collection<Queue<T>> memberQueues;
  2.     final Lambda<T, Comparable> keyGetter;
  3.    
  4.     public T get(boolean remove)
  5.     {
  6.         // workaround to ensure thread-safety when synchronized locks can't extend past the block they're declared in.
  7.        
  8.         // Work on a copy of memberQueues
  9.         List<Queue<T>> memberQueuesCopy;
  10.        
  11.         synchronized(memberQueues)
  12.         { memberQueuesCopy = new ArrayList<Queue<T>>(memberQueues); }
  13.        
  14.         // Declare variables and initialise with last member of memberQueuesCopy.
  15.         // If it checks every variable and the last one is next, then I don't think I need to check again.
  16.         Queue<T> nextQueue = memberQueuesCopy.get(memberQueuesCopy.size() - 1);
  17.         T nextValue = nextQueue.peek();
  18.         Comparable nextValueComparable = keyGetter.getMember(nextValue);
  19.        
  20.         // Check all members of memberQueuesCopy. Find the lowest and hold the value until it gets to it again incase
  21.         // any values have changed. When it gets back to the current lowest value, check whether the value it's being
  22.         // sorted by has changed - and if it has, use its new values and run through all members again. If it hasn't,
  23.         // return it.
  24.        
  25.         // I want a less contrived method. Suggestions that maintain thread safety?
  26.         for(;;)
  27.         {
  28.             for(Queue<T> i : memberQueuesCopy)
  29.             {
  30.                 synchronized(i)
  31.                 {
  32.                     T iValue = i.peek();
  33.                     Comparable iComparable = keyGetter.getMember(iValue);
  34.                    
  35.                     if(i == nextQueue)
  36.                     {
  37.                         if(nextValue == iValue && nextValueComparable.equals(iComparable))
  38.                         {
  39.                             if(remove)
  40.                                 return nextQueue.remove();
  41.                             else
  42.                                 return iValue;
  43.                         }
  44.                        
  45.                         nextValue = nextQueue.peek();
  46.                         nextValueComparable = keyGetter.getMember(nextValue);
  47.                     }
  48.                     else
  49.                     {
  50.                         if(iComparable.compareTo(nextValueComparable) < 0)
  51.                         {
  52.                             nextQueue = i;
  53.                             nextValue = iValue;
  54.                             nextValueComparable = iComparable;
  55.                         }
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement