Java: Stopping a thread that has run for too long?

Posted by Thomas King on Stack Overflow See other posts from Stack Overflow or by Thomas King
Published on 2010-03-22T22:07:29Z Indexed on 2010/03/22 22:11 UTC
Read the original article Hit count: 285

Filed under:
|

Say I've got something like this

public void run(){
    Thread behaviourThread = new Thread(abstractBehaviours[i]);
    behaviourThread.start();
}

And I want to wait until abstractBehaviours[i] run method has either finished or run for 5000 milliseconds. How do I do that? behaviourThread.join(5000) doesn't seem to do that afaik (something is wrong with my code and I've put it down to that).

All the abstract abstractBehaviour class is of course Runnable. I don't want to implement it inside each run method as that seems ugly and there are many different behaviours, I'd much rather have it in the calling/executing thread and do it just once.

Solutions? First time doing something as threaded as this. Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading