KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Mutex


1 /*
2  * Mutex.java
3  *
4  * Copyright (C) 2002-2004 Peter Graves
5  * $Id: Mutex.java,v 1.2 2004/09/09 11:13:19 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 public final class Mutex
25 {
26     private boolean inUse;
27
28     public final boolean isInUse()
29     {
30         return inUse;
31     }
32
33     public void acquire() throws InterruptedException JavaDoc
34     {
35         if (Thread.interrupted())
36             throw new InterruptedException JavaDoc();
37         synchronized(this) {
38             try {
39                 while (inUse)
40                     wait();
41                 inUse = true;
42             }
43             catch (InterruptedException JavaDoc e) {
44                 notify();
45                 throw e;
46             }
47         }
48     }
49
50     public synchronized void release()
51     {
52         if (inUse) {
53             inUse = false;
54             notify();
55         } else
56              Debug.bug("Mutex.release() mutex not in use!");
57     }
58
59     public boolean attempt() throws InterruptedException JavaDoc
60     {
61         return attempt(0);
62     }
63
64     public boolean attempt(long msecs) throws InterruptedException JavaDoc
65     {
66         if (Thread.interrupted())
67             throw new InterruptedException JavaDoc();
68         synchronized(this) {
69             if (!inUse) {
70                 inUse = true;
71                 return true;
72             } else if (msecs <= 0) {
73                 return false;
74             } else {
75                 long waitTime = msecs;
76                 long start = System.currentTimeMillis();
77                 try {
78                     for (;;) {
79                         wait(waitTime);
80                         if (!inUse) {
81                             inUse = true;
82                             return true;
83                         } else {
84                             waitTime = msecs - (System.currentTimeMillis() - start);
85                             if (waitTime <= 0)
86                                 return false;
87                         }
88                     }
89                 }
90                 catch (InterruptedException JavaDoc e) {
91                     notify();
92                     throw e;
93                 }
94             }
95         }
96     }
97 }
98
Popular Tags