001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v1.lang;
024:
025: /**
026: * an abstract Thread class that provides
027: * utilities for easily defining Threads with
028: * safe versions of the deprecated thread
029: * methods stop(), resume(), and start()
030: */
031: public abstract class GentleThread extends Thread {
032: boolean should_stop = false;
033: boolean should_suspend = false;
034:
035: public GentleThread() {
036: super ();
037: }
038:
039: public GentleThread(String name) {
040: super (name);
041: }
042:
043: public abstract void run();
044:
045: /**
046: * a safe method for stopping properly implemented GentleThreads
047: */
048: public synchronized void gentleStop() {
049: should_stop = true;
050: }
051:
052: /**
053: * a safe method for suspending properly implemented GentleThreads
054: */
055: public synchronized void gentleSuspend() {
056: should_suspend = true;
057: }
058:
059: /**
060: * a safe method for resuming properly implemented GentleThreads
061: */
062: public synchronized void gentleResume() {
063: should_suspend = false;
064: this .notifyAll();
065: }
066:
067: /**
068: * tests whether the thread should stop.
069: * Subclasses should call this method periodically in
070: * their run method, and return from run() is the
071: * method returns true.
072: */
073: protected synchronized boolean shouldStop() {
074: return should_stop;
075: }
076:
077: /**
078: * tests whether the thread should suspend.
079: * Subclasses rarely call this method directly,
080: * and should call allowSuspend() periodically
081: * instead.
082: *
083: * @see #allowSuspend
084: */
085: protected synchronized boolean shouldSuspend() {
086: return should_suspend;
087: }
088:
089: /**
090: * tests whether the thread should suspend,
091: * and causes to the thread to pause if appropriate.
092: * Subclasses should call this method periodically
093: * in their run method to, um, allow suspension.
094: * Threads paused by allowSuspend() will be properly
095: * awoken by gentleResume()
096: *
097: * @see #gentleResume
098: */
099: protected synchronized void allowSuspend()
100: throws InterruptedException {
101: while (should_suspend)
102: this.wait();
103: }
104: }
|