KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v1 > lang > GentleThread


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software 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 Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v1.lang;
25
26 /**
27  * an abstract Thread class that provides
28  * utilities for easily defining Threads with
29  * safe versions of the deprecated thread
30  * methods stop(), resume(), and start()
31  */

32 public abstract class GentleThread extends Thread JavaDoc
33 {
34     boolean should_stop = false;
35     boolean should_suspend = false;
36     
37     public GentleThread()
38     { super(); }
39
40     public GentleThread(String JavaDoc name)
41     { super( name ); }
42
43     public abstract void run();
44
45     /**
46      * a safe method for stopping properly implemented GentleThreads
47      */

48     public synchronized void gentleStop()
49     {should_stop = true;}
50
51     /**
52      * a safe method for suspending properly implemented GentleThreads
53      */

54     public synchronized void gentleSuspend()
55     {should_suspend = true;}
56
57     /**
58      * a safe method for resuming properly implemented GentleThreads
59      */

60     public synchronized void gentleResume()
61     {
62     should_suspend = false;
63     this.notifyAll();
64     }
65
66     /**
67      * tests whether the thread should stop.
68      * Subclasses should call this method periodically in
69      * their run method, and return from run() is the
70      * method returns true.
71      */

72     protected synchronized boolean shouldStop()
73     {return should_stop;}
74
75     /**
76      * tests whether the thread should suspend.
77      * Subclasses rarely call this method directly,
78      * and should call allowSuspend() periodically
79      * instead.
80      *
81      * @see #allowSuspend
82      */

83     protected synchronized boolean shouldSuspend()
84     {return should_suspend;}
85
86     /**
87      * tests whether the thread should suspend,
88      * and causes to the thread to pause if appropriate.
89      * Subclasses should call this method periodically
90      * in their run method to, um, allow suspension.
91      * Threads paused by allowSuspend() will be properly
92      * awoken by gentleResume()
93      *
94      * @see #gentleResume
95      */

96     protected synchronized void allowSuspend() throws InterruptedException JavaDoc
97     {while (should_suspend) this.wait();}
98 }
99
Popular Tags