Controlling a thread's processing time
Hey there. I know this subject is found everywhere, but I really couldn't find an answer to my problem... yet :)
I have to support Runnable object not written by me, so I don't have access to their source code (decompiling them is not an option). So, I can't rewrite the run() method. Example:
publicclass MyClassextends Runnable{
...
publicvoid run(){
sleep(10000);//sleep for 10 seconds
}
...
}
I need to do something like
new Thread(new MyClass()).start();
make it run and, if it takes longer than x seconds, then stop them no matter what, even if they leave some corrupt variables.
I tried plenty of things but couldn't make it work. Pseudo code could be somthing like:
Thread t =new Thread(new Myclass());
t.start();
Waitfor t to terminatefor 3 seconds;//note that I can't use t.wait(3000), since I can't call notify() from inside t.
If (t didn't finalize yet)
Kill the bastard and System.out.println("Forced to terminate!");
else
System.out.println("Great job!");
Also, if the thread ends before the 3 seconds period, I would like not to keep waiting for the 3 seconds. That is, I don't want to call Thread.sleep(3000) after t.start();
Any piece of advise here? Thanks in advance!

