Is there any API or options wherein lines of codes are executed within a specified time.
code line 1
code line 2
code line 3
code line 4 ---- The code execution stops here itself without proceeding since the given time limit has ended.
code line 5
EndTimer<Current time+10 seconds>
Run the code in a different thread, then use join:
There is no real way of actually terminating the thread though, so the code will continue in the background. Maybe interrupting the thread can help but I'm not sure.
Alternatively, check out java.util.concurrent.Future. That's a class with a timed get() method that can be canceled.