In fact I would execute a specific task( a set of instructions) for a determined period.
For example : I want my program to execute the task for 5 minutes, if it gets the right result it stops , else it will continue executing normal task for the 5 minutes and in the end it tells me.
How can I implement this in Java.
You could something like the following:
Javadocs for ExecutorService are here
[edit]
I should probably note however that depending on what your long running task is doing, it may not be possible to force it to stop running
[edit2] the submit method returns a
Futureobject, which you can then callgeton with a timeout. Thisgetcall will block until a result is ready, or if the timeout is reached throw a TimeoutException. In this way, you can get a result back from your long running task if that is what you wanted