1 19 20 package org.netbeans.lib.editor.util; 21 22 37 38 public class PriorityMutex { 39 40 private Thread lockThread; 41 42 private int lockDepth; 43 44 private Thread waitingPriorityThread; 45 46 60 public synchronized void lock() { 61 Thread thread = Thread.currentThread(); 62 if (thread != lockThread) { while (lockThread != null 66 || (waitingPriorityThread != null && waitingPriorityThread != thread) 67 ) { 68 try { 69 if (waitingPriorityThread == null && isPriorityThread()) { 70 waitingPriorityThread = thread; 71 } 72 73 wait(); 74 75 } catch (InterruptedException e) { 76 waitingPriorityThread = null; 77 } 78 } 79 80 lockThread = thread; 81 82 if (thread == waitingPriorityThread) { 83 waitingPriorityThread = null; } 85 } 86 87 lockDepth++; 88 } 89 90 95 public synchronized void unlock() { 96 if (Thread.currentThread() != lockThread) { 97 throw new IllegalStateException ("Not locker"); } 99 100 if (--lockDepth == 0) { 101 lockThread = null; 102 103 notifyAll(); } 105 } 106 107 120 public boolean isPriorityThreadWaiting() { 121 return (waitingPriorityThread != null); 122 } 123 124 133 public final synchronized Thread getLockThread() { 134 return lockThread; 135 } 136 137 149 protected boolean isPriorityThread() { 150 return javax.swing.SwingUtilities.isEventDispatchThread(); 151 } 152 153 } 154 | Popular Tags |