KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > HaltingThread


1 /*
2
3    Copyright 2004 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17 */

18
19 package org.apache.batik.util;
20
21 /**
22  * This is a subclass of java.lang.Thread that includes a non-intrusive
23  * 'halt' method. The Halt method simply sets a boolean that can be
24  * checked periodically during expensive processing.
25  *
26  * @author <a HREF="mailto:deweese@apache.org">deweese</a>
27  * @version $Id: HaltingThread.java,v 1.2 2005/03/27 08:58:36 cam Exp $
28  */

29 public class HaltingThread extends Thread JavaDoc {
30     /**
31      * Boolean indicating if this thread has ever been 'halted'.
32      */

33     protected boolean beenHalted = false;
34
35     public HaltingThread() { }
36
37     public HaltingThread(Runnable JavaDoc r) { super(r); }
38
39     public HaltingThread(String JavaDoc name) { super(name); }
40
41     public HaltingThread(Runnable JavaDoc r, String JavaDoc name) { super(r, name); }
42
43     /**
44      * returns true if someone has halted the thread.
45      */

46     public boolean isHalted() {
47         synchronized (this) { return beenHalted; }
48     }
49
50     /**
51      * Set's beenHalted to true.
52      */

53     public void halt() {
54         synchronized (this) { beenHalted = true; }
55     }
56
57     /**
58      * Set's beenHalted to false.
59      */

60     public void clearHalted() {
61         synchronized (this) { beenHalted = false; }
62     }
63
64     /**
65      * Calls 'halt' on <tt>Thread.currentThread()</tt> if it is an
66      * instance of HaltingThread otherwise it does nothing.
67      */

68     public static void haltThread() {
69         haltThread(Thread.currentThread());
70     }
71
72     /**
73      * Calls 'halt' on <tt>t</tt> if it is an instance of
74      * HaltingThread otherwise it does nothing.
75      */

76     public static void haltThread(Thread JavaDoc t) {
77         if (t instanceof HaltingThread)
78             ((HaltingThread)t).halt();
79     }
80
81     /**
82      * Returns the result of calling hasBeenHalted on
83      * <tt>Thread.currentThread()</tt>, if it is an instance of
84      * HaltingThread otherwise it returns false.
85      */

86     public static boolean hasBeenHalted() {
87         return hasBeenHalted(Thread.currentThread());
88     }
89
90     /**
91      * Returns the result of calling hasBeenHalted on <tt>t</tt>,
92      * if it is an instance of HaltingThread otherwise it returns false.
93      */

94     public static boolean hasBeenHalted(Thread JavaDoc t) {
95         if (t instanceof HaltingThread)
96             return ((HaltingThread)t).isHalted();
97         return false;
98     }
99
100
101 };
102
Popular Tags