1 19 20 package org.netbeans.editor.view.spi; 21 22 37 38 public class ViewHierarchyMutex { 39 40 private Thread lockThread; 41 42 private int lockDepth; 43 44 private Thread waitingPriorityThread; 45 46 public synchronized void lock() { 47 Thread thread = Thread.currentThread(); 48 boolean priorityThread = isPriorityThread(thread); 49 50 if (thread != lockThread) { while (lockThread != null 54 || (waitingPriorityThread != null && waitingPriorityThread != thread) 55 ) { 56 try { 57 if (waitingPriorityThread == null && priorityThread) { 58 waitingPriorityThread = thread; 59 } 60 61 wait(); 62 63 } catch (InterruptedException e) { 64 waitingPriorityThread = null; 65 } 66 } 67 68 lockThread = thread; 69 70 if (thread == waitingPriorityThread) { 71 waitingPriorityThread = null; } 73 } 74 75 lockDepth++; 76 } 77 78 public synchronized void unlock() { 79 if (Thread.currentThread() != lockThread) { 80 throw new IllegalStateException ("Not locker"); } 82 83 if (--lockDepth == 0) { 84 lockThread = null; 85 86 notifyAll(); } 88 } 89 90 103 public boolean isPriorityThreadWaiting() { 104 return (waitingPriorityThread != null); 105 } 106 107 protected boolean isPriorityThread(Thread thread) { 108 return (thread != ViewLayoutQueue.getDefaultQueue().getWorkerThread()); 109 } 110 111 121 public final synchronized Thread getLockThread() { 122 return lockThread; 123 } 124 125 } 126 | Popular Tags |