KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > thread > ThreadStateMonitor


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ThreadStateMonitor.java
20  *
21  * This object is responsible for monitoring the state of a thread.
22  */

23
24 package com.rift.coad.lib.thread;
25
26 /**
27  * This object is responsible for monitoring the state of a thread.
28  *
29  * @author Brett Chaldecott
30  */

31 public class ThreadStateMonitor {
32     
33     // the private member variables
34
private boolean terminated = false;
35     private long delay = 0;
36     
37     /**
38      * Creates a new instance of ThreadStateMonitor
39      */

40     public ThreadStateMonitor() {
41     }
42     
43     
44     /**
45      * The constructor that sets the delay time for the thread state monitor.
46      *
47      * @param delay The length of time the thread should what before continuing
48      * to the next processing iteration.
49      */

50     public ThreadStateMonitor(long delay) {
51         this.delay = delay;
52     }
53     
54     
55     /**
56      * This method returns true if this object is terminated and false if it
57      * is not.
58      *
59      * @return TRUE if terminated, FALSE if not.
60      */

61     public synchronized boolean isTerminated() {
62         return terminated;
63     }
64     
65     
66     /**
67      * This method sets the terminated flag to true and notifies waiting threads
68      * of this fact.
69      *
70      * @param broadCast Set to true if there can be more than one thread blocking.
71      */

72     public synchronized void terminate(boolean broadCast) {
73         terminated = true;
74         if (broadCast) {
75             notifyAll();
76         } else {
77             notify();
78         }
79     }
80     
81     
82     /**
83      * This method will get called to monitor the state of the thread.
84      */

85     public synchronized void monitor() {
86         try {
87             if (terminated) {
88                 return;
89             }
90             if (delay > 0) {
91                 wait(delay);
92             } else {
93                 wait();
94             }
95         } catch (Exception JavaDoc ex) {
96             // ignore
97
}
98     }
99 }
100
Popular Tags