KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > util > PriorityMutex


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.editor.util;
21
22 /**
23  * Mutex that allows only one thread to proceed
24  * other threads must wait until that one finishes.
25  * <br>
26  * The thread that "holds" the mutex (has the mutex access granted)
27  * may reenter the mutex arbitrary number of times
28  * (just increasing a "depth" of the locking).
29  * <br>
30  * If the priority thread enters waiting on the mutex
31  * then it will get serviced first once the current thread
32  * leaves the mutex.
33  *
34  * @author Miloslav Metelka
35  * @version 1.00
36  */

37
38 public class PriorityMutex {
39
40     private Thread JavaDoc lockThread;
41
42     private int lockDepth;
43     
44     private Thread JavaDoc waitingPriorityThread;
45     
46     /**
47      * Acquire the ownership of the mutex.
48      *
49      * <p>
50      * The following pattern should always be used:
51      * <pre>
52      * mutex.lock();
53      * try {
54      * ...
55      * } finally {
56      * mutex.unlock();
57      * }
58      * </pre>
59      */

60     public synchronized void lock() {
61         Thread JavaDoc thread = Thread.currentThread();
62         if (thread != lockThread) { // not nested locking
63
// Will wait if either there is another thread already holding the lock
64
// or if there is a priority thread waiting but it's not this thread
65
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 JavaDoc e) {
76                     waitingPriorityThread = null;
77                 }
78             }
79
80             lockThread = thread;
81
82             if (thread == waitingPriorityThread) {
83                 waitingPriorityThread = null; // it's now allowed to enter
84
}
85         }
86
87         lockDepth++;
88     }
89     
90     /**
91      * Release the ownership of the mutex.
92      *
93      * @see #lock()
94      */

95     public synchronized void unlock() {
96         if (Thread.currentThread() != lockThread) {
97             throw new IllegalStateException JavaDoc("Not locker"); // NOI18N
98
}
99
100         if (--lockDepth == 0) {
101             lockThread = null;
102
103             notifyAll(); // must all to surely notify waitingPriorityThread too
104
}
105     }
106     
107     /**
108      * Can be called by the thread
109      * that acquired the mutex to check whether there
110      * is a priority thread (such as AWT event-notification thread)
111      * waiting.
112      * <br>
113      * If there is a priority thread waiting the non-priority thread
114      * should attempt to stop its work as soon and release the ownership
115      * of the mutex.
116      * <br>
117      * The method must *not* be called without first taking the ownership
118      * of the mutex (it is intentionally not synchronized).
119      */

120     public boolean isPriorityThreadWaiting() {
121         return (waitingPriorityThread != null);
122     }
123     
124     /**
125      * Return a thread that acquired this mutex.
126      * <br>
127      * This method is intended for diagnostic purposes only.
128      *
129      * @return thread that currently acquired lock the mutex
130         or <code>null</code>
131      * if there is currently no thread holding that acquired this mutex.
132      */

133     public final synchronized Thread JavaDoc getLockThread() {
134         return lockThread;
135     }
136
137     /**
138      * Return true if the current thread that is entering this method
139      * is a priority thread
140      * and should be allowed to enter as soon as possible.
141      *
142      * <p>
143      * The default implementation assumes that
144      * {@link javax.swing.SwingUtilities#isEventDispatchThread()}
145      * is a priority thread.
146      *
147      * @return true if the entering thread is a priority thread.
148      */

149     protected boolean isPriorityThread() {
150         return javax.swing.SwingUtilities.isEventDispatchThread();
151     }
152
153 }
154
Popular Tags