Stopping a thread after a time
Hi,
I'd like to be able to run a thread for a certain time and, based on it's state, then either run it for a longer time or suspend it. It'd be a bit awkward to do this in the thread itself, so I was thinking it'd be best to have another timer thread that interrupted it after n seconds. Unfortunately I'm not sure how to do this, despite a lot of searching.
Cheers,
[391 byte] By [
syfaxa] at [2007-11-26 18:54:47]

> I'd like to be able to run a thread for a certain
> time and, based on it's state, then either run it for
> a longer time or suspend it. It'd be a bit awkward to
> do this in the thread itself, so I was thinking it'd
> be best to have another timer thread that interrupted
> it after n seconds. Unfortunately I'm not sure how to
> do this, despite a lot of searching.
If the decision to run it longer is based on it's state, then I makes more sense for the thread to determine if it should keep running or not (rather than another object).
Anyway, if something else was going to determine this for it, then why not just create a method on the thread that would stop it, or run it longer?
you should create a master thread, which spawns other threads.the master thread would then be given some control over the other threads.
Ok so the thread involves recursively searching a (very) large tree. To make it work directly, I'd have to do some test on each recursive call, and then make the return value include some parameter to indicate whether it was due to a time out. There'd be some overhead for this, and it wouldn't lead to very pretty code.
There is some form of 'master thread' - so how would I implement this using that?
*Hopeful bump*The only other idea I've had is to try installing AspectJ and putting a pointcut that checks the time on the recursive call. I've never used them though, so no idea if this would work.
> Ok so the thread involves recursively searching a
> (very) large tree. To make it work directly, I'd have
> to do some test on each recursive call, and then make
> the return value include some parameter to indicate
> whether it was due to a time out. There'd be some
> overhead for this, and it wouldn't lead to very
> pretty code.
You'd probably be better off throwing an expection. As in:
if(System.currentTimeMillis() > quitingTime)
throw MyTimeoutException();
You could run a timeout as a separate thread or timer, but it would still have to set a flag which you would have to test, so the difference in overhead would be minor.
create a master thread at the root of the tree.
this thread would then spawn a thread at each branch, each one these threads would spawn a thread at each branch, and so on.; when each thread reached the eand of its branch it would report that it had finished.
you could not rely on them finishing in any particular order however,
there is no 'master thread' as such it upto you how you implemement the thread control.
You could try something like this ...
public class ThreadToThread {
public static void main(String[] argv) {
ThreadToThread ttt = new ThreadToThread();
TempThread tt = ttt.new TempThread();
tt.start();
for (int i = 1; i < 11; i++) {
try {
Thread.sleep(5000);
System.out.println("loop #"+i);
if (tt.isDone())
break;
}
catch(InterruptedException ie) {
System.out.println("In main: "+ie);
}
}
if (!tt.isDone())
tt.setDone();
System.out.println("Done");
}
class TempThread extends Thread {
boolean done;
longl;
public void run() {
while ( !done ) {
doSomething();
}
}
private void doSomething() {
longi = 0;
double j = 0;
System.out.println("In doSomething ... starting calculations ... ");
for (; i < 50000000; i++) {
j = ((((j / 2.005) * .0075) % 10000.25) * Math.random()*10 * Math.sqrt(i));
}
done = true;
System.out.println("In TempThread ... done!");
}
private boolean isDone() {
return( done );
}
private void setDone() {
System.out.println("Setting done to true");
done = true;
}
}
}