KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > concurrency > lib > Semaphore


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

18 package org.objectweb.perseus.concurrency.lib;
19
20 /**
21  * A semaphore which can be disable at the definition time.
22  *
23  * @author S.Chassande-Barrioz
24  */

25 public class Semaphore {
26
27     private Object JavaDoc user;
28     private boolean checkTread;
29     private boolean reentrant = false;
30
31     public boolean on;
32
33     public Semaphore() {
34         this(true);
35     }
36
37     public Semaphore(boolean on) {
38         this(on, true);
39     }
40     public Semaphore(boolean on, boolean checkthread) {
41         this(on, checkthread, false);
42     }
43
44     public Semaphore(boolean on, boolean checkthread, boolean reentrant) {
45         this.on = on;
46         this.checkTread = checkthread;
47         this.reentrant = reentrant;
48         user = null;
49     }
50
51     public void init(boolean isOn) {
52         on = isOn;
53         if (isOn) {
54             user = null;
55         }
56     }
57
58     public boolean isActive() {
59         return on;
60     }
61
62     public boolean P() {
63         if (on) {
64             synchronized (this) {
65                 if (!reentrant || this.user != Thread.currentThread()) {
66                     while (user != null) {
67                         try {
68                             wait();
69                         } catch (InterruptedException JavaDoc e) {
70                         }
71                     }
72                     this.user = Thread.currentThread();
73                 }
74             }
75         }
76         return on;
77     }
78
79     public Object JavaDoc getUser() {
80         return user;
81     }
82
83     public void V() {
84         if (on) {
85             synchronized (this) {
86                 if (!checkTread || Thread.currentThread() == user) {
87                     user = null;
88                     notify();
89                 } else {
90                     new RuntimeException JavaDoc("Not owner expected:" + user
91                         + ", found: " + Thread.currentThread());
92                 }
93             }
94         }
95     }
96 }
97
Popular Tags