Is it possible to set a timeout for a set of instructions in java? I have the following:
new Thread(new Runnable(){
public void run() {
//instructions
for(...){
....
}
//instructions2
}}).start();
i would like to set a timeout for the loop for and if it reaches the time continue normally with instructions2. Inside the loop i have several function calls( a litle complex organization)and can get blocked inside any one of them, resulting in long duration loop.
Thanks in advance.
Assuming your blocking functions respond to interruption, you could use a future with a timeout. If they don’t there is not much you can do… Note that with the approach below you don’t need to manually start the thread any more.