001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019:
020: package org.apache.batik.util;
021:
022: /**
023: * This is a subclass of java.lang.Thread that includes a non-intrusive
024: * 'halt' method. The Halt method simply sets a boolean that can be
025: * checked periodically during expensive processing.
026: *
027: * @author <a href="mailto:deweese@apache.org">deweese</a>
028: * @version $Id: HaltingThread.java 478169 2006-11-22 14:23:24Z dvholten $
029: */
030: public class HaltingThread extends Thread {
031: /**
032: * Boolean indicating if this thread has ever been 'halted'.
033: */
034: protected boolean beenHalted = false;
035:
036: public HaltingThread() {
037: }
038:
039: public HaltingThread(Runnable r) {
040: super (r);
041: }
042:
043: public HaltingThread(String name) {
044: super (name);
045: }
046:
047: public HaltingThread(Runnable r, String name) {
048: super (r, name);
049: }
050:
051: /**
052: * returns true if someone has halted the thread.
053: */
054: public boolean isHalted() {
055: synchronized (this ) {
056: return beenHalted;
057: }
058: }
059:
060: /**
061: * Set's beenHalted to true.
062: */
063: public void halt() {
064: synchronized (this ) {
065: beenHalted = true;
066: }
067: }
068:
069: /**
070: * Set's beenHalted to false.
071: */
072: public void clearHalted() {
073: synchronized (this ) {
074: beenHalted = false;
075: }
076: }
077:
078: /**
079: * Calls 'halt' on <tt>Thread.currentThread()</tt> if it is an
080: * instance of HaltingThread otherwise it does nothing.
081: */
082: public static void haltThread() {
083: haltThread(Thread.currentThread());
084: }
085:
086: /**
087: * Calls 'halt' on <tt>t</tt> if it is an instance of
088: * HaltingThread otherwise it does nothing.
089: */
090: public static void haltThread(Thread t) {
091: if (t instanceof HaltingThread)
092: ((HaltingThread) t).halt();
093: }
094:
095: /**
096: * Returns the result of calling hasBeenHalted on
097: * <tt>Thread.currentThread()</tt>, if it is an instance of
098: * HaltingThread otherwise it returns false.
099: */
100: public static boolean hasBeenHalted() {
101: return hasBeenHalted(Thread.currentThread());
102: }
103:
104: /**
105: * Returns the result of calling hasBeenHalted on <tt>t</tt>,
106: * if it is an instance of HaltingThread otherwise it returns false.
107: */
108: public static boolean hasBeenHalted(Thread t) {
109: if (t instanceof HaltingThread)
110: return ((HaltingThread) t).isHalted();
111: return false;
112: }
113:
114: }
|