I think the topic might not be accurate enough, but I really don't know how to describe it in a very brief way...
What I want to do is as follows: I have a process running some analysis (in Java) and trying to give a return value, but the whole analysis has a time constraint, say 10 seconds
If the analysis gives a result within 10s, return the result; else, times up when reaching 10s, stop the process and return a dummy value
I am a newbie in this field, any suggestions will be welcomed.
You can use FutureTask to create a job and request the result within a particular time (using get() with a timeout). If that time window is breached, then you'll get an exception.
At that point you can then call the cancel() method with the parameter set to true to force an interruption and cancel the underlying task.
Object result = DUMMY;
try {
result = futureTask.get(5, TimeOut.SECONDS);
}
catch (TimeoutException e) {
futureTask.cancel(true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With