Modified code to handle an exception that propagates up past iterate(). It
still causes the thread to exit by default, but it provides a mechanism for overriding that behavior and it logs the exception rather than quietly dying. git-svn-id: https://samskivert.googlecode.com/svn/trunk@334 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: LoopingThread.java,v 1.3 2001/08/11 22:43:29 mdb Exp $
|
// $Id: LoopingThread.java,v 1.4 2001/10/02 22:35:26 mdb Exp $
|
||||||
//
|
//
|
||||||
// samskivert library - useful routines for java programs
|
// samskivert library - useful routines for java programs
|
||||||
// Copyright (C) 2001 Michael Bayne
|
// Copyright (C) 2001 Michael Bayne
|
||||||
@@ -20,6 +20,8 @@
|
|||||||
|
|
||||||
package com.samskivert.util;
|
package com.samskivert.util;
|
||||||
|
|
||||||
|
import com.samskivert.Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The looping thread provides the basic functionality for a thread that
|
* The looping thread provides the basic functionality for a thread that
|
||||||
* does its processing in a simple loop. This is characteristic of most
|
* does its processing in a simple loop. This is characteristic of most
|
||||||
@@ -45,9 +47,8 @@ public class LoopingThread extends Thread
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processes the loop. Derived classes should override
|
* Processes the loop. Derived classes should override {@link iterate}
|
||||||
* <code>iterate()</code> to perform whatever action needs to be
|
* to perform whatever action needs to be performed inside the loop.
|
||||||
* performed inside the loop.
|
|
||||||
*/
|
*/
|
||||||
public void run ()
|
public void run ()
|
||||||
{
|
{
|
||||||
@@ -55,7 +56,11 @@ public class LoopingThread extends Thread
|
|||||||
willStart();
|
willStart();
|
||||||
|
|
||||||
while (isRunning()) {
|
while (isRunning()) {
|
||||||
iterate();
|
try {
|
||||||
|
iterate();
|
||||||
|
} catch (Exception e) {
|
||||||
|
handleIterateFailure(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
@@ -65,8 +70,8 @@ public class LoopingThread extends Thread
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Called to wake the thread up from any blocking wait that it might
|
* Called to wake the thread up from any blocking wait that it might
|
||||||
* be in. This function should result in the thread exiting the
|
* be in. This function should result in the thread exiting the {@link
|
||||||
* iterate() function as soon as possible so that the running flag can
|
* iterate} function as soon as possible so that the running flag can
|
||||||
* be checked and the thread can cleanly exit.
|
* be checked and the thread can cleanly exit.
|
||||||
*/
|
*/
|
||||||
protected void kick ()
|
protected void kick ()
|
||||||
@@ -76,8 +81,8 @@ public class LoopingThread extends Thread
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Called before the thread enters the processing loop. Any
|
* Called before the thread enters the processing loop. Any
|
||||||
* initialization that needs to be performed prior to the
|
* initialization that needs to be performed prior to the {@link
|
||||||
* <code>iterate()</code> loop can be done here.
|
* iterate} loop can be done here.
|
||||||
*/
|
*/
|
||||||
protected void willStart ()
|
protected void willStart ()
|
||||||
{
|
{
|
||||||
@@ -85,19 +90,35 @@ public class LoopingThread extends Thread
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the main body of the loop. It will be called over and over
|
* This is the main body of the loop. It will be called over and over
|
||||||
* again until the thread is requested to exit via a call to
|
* again until the thread is requested to exit via a call to {@link
|
||||||
* <code>shutdown()</code>. At minimum, a derived class must override
|
* shutdown}. At minimum, a derived class must override this function
|
||||||
* this function and do something useful.
|
* and do something useful.
|
||||||
*/
|
*/
|
||||||
protected void iterate ()
|
protected void iterate ()
|
||||||
{
|
{
|
||||||
throw new RuntimeException("Derived class must implement iterate().");
|
throw new RuntimeException("Derived class must implement iterate().");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If an exception propagates past {@link iterate}, it will be caught
|
||||||
|
* and supplied to this function, which may want to do something
|
||||||
|
* useful with it. Presently, the exception is logged and the thread
|
||||||
|
* is allowed to terminate.
|
||||||
|
*/
|
||||||
|
protected void handleIterateFailure (Exception e)
|
||||||
|
{
|
||||||
|
// log the exception
|
||||||
|
Log.warning("LoopingThread.iterate() uncaught exception.");
|
||||||
|
Log.logStackTrace(e);
|
||||||
|
|
||||||
|
// and shut the thread down
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called after the thread has been requested to exit. Any cleanup
|
* Called after the thread has been requested to exit. Any cleanup
|
||||||
* that should take place after the <code>iterate()</code> loop has
|
* that should take place after the {@link iterate} loop has exited
|
||||||
* exited can be done here.
|
* can be done here.
|
||||||
*/
|
*/
|
||||||
protected void didShutdown ()
|
protected void didShutdown ()
|
||||||
{
|
{
|
||||||
@@ -105,10 +126,10 @@ public class LoopingThread extends Thread
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates whether or not the thread should still be running. If a
|
* Indicates whether or not the thread should still be running. If a
|
||||||
* thread is calling this within <code>iterate()</code>, it should
|
* thread is calling this within {@link iterate}, it should exit
|
||||||
* exit quickly and cleanly if the function returns false. It is
|
* quickly and cleanly if the function returns false. It is
|
||||||
* automatically called as part of the iterate() loop, so normally a
|
* automatically called as part of the {@link iterate} loop, so
|
||||||
* derived-class won't have to call it.
|
* normally a derived-class won't have to call it.
|
||||||
*/
|
*/
|
||||||
protected synchronized boolean isRunning ()
|
protected synchronized boolean isRunning ()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user