KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > base > Mutex


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.base;
17
18 public class Mutex {
19     private boolean mutex;
20
21     public Mutex() {
22         mutex = false;
23     }
24
25     /**
26      *
27      * @return true the mutex was indeed taken anew, false if calling thread already had mutex.
28      */

29     public synchronized void lock() {
30         while (mutex) {
31             try {
32                 wait();
33             } catch (InterruptedException JavaDoc e) {
34                 if (Thread.currentThread().isInterrupted()) {
35                     // gota go now
36
throw new RuntimeException JavaDoc(e);
37                 }
38
39                 // else keep waiting
40
}
41         }
42
43         mutex = true;
44     }
45
46     public synchronized void release() {
47         mutex = false;
48         notify();
49     }
50 }
51
Popular Tags