KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > TIR > Impl > Lock


1 /* $Id: Lock.java,v 1.1.1.1 2003/02/11 16:19:41 bures Exp $ */
2 package SOFA.SOFAnode.Made.TIR.Impl;
3
4 class Lock {
5   boolean locked;
6
7   public Lock(boolean l) {
8     locked = l;
9   }
10
11   synchronized public boolean lock() {
12     try {
13       while (locked)
14         wait(); /* wait until somenone call notify() */
15       locked = true;
16     } catch (InterruptedException JavaDoc e) {
17       return false;
18     }
19     return true;
20   }
21
22   /* return false, if can't get lock immediatelly */
23   synchronized public boolean tryLock() {
24     if (locked)
25       return false;
26     else {
27       locked = true;
28       return true;
29     }
30   }
31
32   synchronized public void unlock() {
33     locked = false;
34     notify(); /* wake up others which wait for lock */
35   }
36
37   synchronized public boolean isLocked() {
38     return locked;
39   }
40 }
41
Popular Tags