PDA

Click to See Complete Forum and Search --> : thread timeout


Pouncer
Jul 21st, 2008, 06:46 AM
i found this on the net, which is a thread timeout:


// TimeKiller - kill a thread after a given timeout has elapsed

import java.io.*;
import java.util.*;

/// Kill a thread after a given timeout has elapsed
// <P>
// A simple timeout class. You give it a thread to watch and a timeout
// in milliseconds. After the timeout has elapsed, the thread is killed
// with a Thread.stop(). If the thread finishes successfully before then,
// you can cancel the timeout with a done() call; you can also re-use the
// timeout on the same thread with the reset() call.
// <P>

public class TimeKiller implements Runnable
{

private Thread targetThread;
private long millis;
private Thread watcherThread;
private boolean loop;
private boolean enabled;

/// Constructor. Give it a thread to watch, and a timeout in milliseconds.
// After the timeout has elapsed, the thread gets killed. If you want
// to cancel the kill, just call done().
public TimeKiller( Thread targetThread, long millis )
{
this.targetThread = targetThread;
this.millis = millis;
watcherThread = new Thread( this );
enabled = true;
watcherThread.start();
// Hack - pause a bit to let the watcher thread get started.
try
{
Thread.sleep( 100 );
}
catch ( InterruptedException e ) {}
}

/// Constructor, current thread.
public TimeKiller( long millis )
{
this( Thread.currentThread(), millis );
}

/// Call this when the target thread has finished.
public synchronized void done()
{
loop = false;
enabled = false;
notify();
}

/// Call this to restart the wait from zero.
public synchronized void reset()
{
loop = true;
notify();
}

/// Call this to restart the wait from zero with a different timeout value.
public synchronized void reset( long millis )
{
this.millis = millis;
reset();
}

/// The watcher thread - from the Runnable interface.
// This has to be pretty anal to avoid monitor lockup, lost
// threads, etc.
public synchronized void run()
{
Thread me = Thread.currentThread();
me.setPriority( Thread.MAX_PRIORITY );
if ( enabled )
{
do
{
loop = false;
try
{
wait( millis );
}
catch ( InterruptedException e ) {}
}
while ( enabled && loop );
}
if ( enabled && targetThread.isAlive() )
targetThread.stop();
}


/******************************************************************************
/// Test routine.
public static void main( String[] args )
{
System.out.println( (new Date()) + " Setting ten-second timeout..." );
TimeKiller tk = new TimeKiller( 10000 );
try
{
System.out.println(
(new Date()) + " Starting twenty-second pause..." );
Thread.sleep( 20000 );
System.out.println(
(new Date()) + " Another twenty-second pause..." );
Thread.sleep( 20000 );
}
catch ( InterruptedException e )
{
System.out.println(
(new Date()) + " Caught InterruptedException" );
}
catch ( ThreadDeath e )
{
System.out.println( (new Date()) + " Caught ThreadDeath" );
throw e;
}
System.out.println( (new Date()) + " Oops - pauses finished!" );
}
******************************************************************************/

}


My thread looks like this:


class ThreadMove implements Runnable {
ComputerPlayerInterface object;
GameData gameData;
long timeout;

public static boolean done = false;

public static GameAnswer gameAnswer = null;

public void msgbox(String message) {
JOptionPane.showMessageDialog(null, message);
}

public ThreadMove(ComputerPlayerInterface o, GameData gd, long t) {
object = o;
gameData = gd;
timeout = t;
}

public void run() {
try {
gameAnswer = object.doPlay(gameData);
done = true;
}

catch (Exception e) {
msgbox("From thread " + e.toString());
}
}
}


Can anyone help me add this timeout into my thread, so the thread will just automatically close after 'timeout' millisecs?

weslei
Aug 7th, 2008, 05:27 PM
Well, it was posted a while ago... But nobody answered.
I hope this can help:

long timeout = 60000;
ThreadMove tmtarget = null;
//initialize ThreadMove here: tmtarget = new ThreadMove(parameters);
Thread tm = new Thread(tmtarget);
Thread t = new Thread(new TimeKiller(tm,timeout));
t.start();