1 13 14 15 package EDU.oswego.cs.dl.util.concurrent; 16 17 31 32 public class PrioritySemaphore extends QueuedSemaphore { 33 34 40 41 42 public PrioritySemaphore(long initialPermits) { 43 super(new PriorityWaitQueue(), initialPermits); 44 } 45 46 protected static class PriorityWaitQueue extends WaitQueue { 47 48 49 50 protected final FIFOSemaphore.FIFOWaitQueue[] cells_ = 51 new FIFOSemaphore.FIFOWaitQueue[Thread.MAX_PRIORITY - 52 Thread.MIN_PRIORITY + 1]; 53 54 58 59 protected int maxIndex_ = -1; 60 61 protected PriorityWaitQueue() { 62 for (int i = 0; i < cells_.length; ++i) 63 cells_[i] = new FIFOSemaphore.FIFOWaitQueue(); 64 } 65 66 protected void insert(WaitNode w) { 67 int idx = Thread.currentThread().getPriority() - Thread.MIN_PRIORITY; 68 cells_[idx].insert(w); 69 if (idx > maxIndex_) maxIndex_ = idx; 70 } 71 72 protected WaitNode extract() { 73 for (;;) { 74 int idx = maxIndex_; 75 if (idx < 0) 76 return null; 77 WaitNode w = cells_[idx].extract(); 78 if (w != null) 79 return w; 80 else 81 --maxIndex_; 82 } 83 } 84 } 85 86 87 88 89 90 } 91 | Popular Tags |