Major cleanup of Utils class.
[trakem2.git] / ini / trakem2 / utils / Lock.java
blob875e4b918ac7bec08bf375acc7fc68ecaa036806
1 /**
3 TrakEM2 plugin for ImageJ(C).
4 Copyright (C) 2005, 2006 Albert Cardona and Rodney Douglas.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation (http://www.gnu.org/licenses/gpl.txt )
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 You may contact Albert Cardona at acardona at ini.phys.ethz.ch
20 Institute of Neuroinformatics, University of Zurich / ETH, Switzerland.
21 **/
23 package ini.trakem2.utils;
25 /** For thread synchronization.
26 * Both methods MUST ALWAYS be called from within a statement synchronizing on this object, such as:<br />
27 * <br />
28 * final Lock lock = new Lock();<br />
29 * synchronized (lock) {<br />
30 * lock.lock();<br />
31 * try {<br />
32 * ... (do whatever needs be synchronized on this lock) ...<br />
33 * } catch (Exception e) {<br />
34 * e.printStackTrace();<br />
35 * }<br />
36 * lock.unlock();<br />
37 * }<br />
39 public class Lock {
40 protected boolean locked = false;
41 protected boolean debug = false;
42 static protected boolean debug_all = false;
43 public final void lock() {
44 if (debug || debug_all) Utils.printCaller(this, 7);
45 while (locked) try { this.wait(); } catch (InterruptedException ie) {}
46 locked = true;
48 public final void unlock() {
49 if (debug || debug_all) Utils.printCaller(this, 7);
50 locked = false;
51 this.notifyAll();
53 public final void debug() {
54 debug = true;
56 static public final void debugAll() {
57 debug_all = true;