KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > CompletionMonitor


1 package edu.rice.cs.util;
2
3 /** Enables threads to communicate with each other by signaling. Typically, this communication concerns a
4  * task which one thread must complete before other threads can proceed.
5  */

6 public class CompletionMonitor {
7   private boolean _flag;
8   
9   public CompletionMonitor(boolean flag) { _flag = flag; }
10   
11   public CompletionMonitor() { this(false); }
12   
13   /** Returns whether the flag is currently set */
14   public synchronized boolean isFlag() { return _flag; }
15   
16   /** Sets the state to signaled, indicating that waiting threads can continue */
17   public synchronized void set() {
18     _flag = true;
19     this.notifyAll();
20   }
21   
22   /** Sets the state to unsignaled */
23   public synchronized void reset() { _flag = false; }
24   
25   /** Causes the calling thread to wait for the signal to be set before continuing
26    * If the signal is already set, it returns immediately
27    * @return returns true, unless the waiting thread was interrupted */

28   public synchronized boolean waitOne() {
29     while (!_flag) {
30       try { this.wait(); }
31       catch (InterruptedException JavaDoc e) { return false; }
32     }
33     return true;
34   }
35 }
36
Popular Tags