I have method that uses ExecutorService to schedule tasks. I need the task to be terminated after a set amount of time. How can I specify such a time?
public String schedule(Callable<Object> task)
{
Future<Object> f = null;
String status = null;
try
{
f = service.submit(task);
status = f.get(1, TimeUnit.SECONDS).toString();
}
catch (TimeoutException e)
{
status = "Waiting took too long.";
}
catch (Exception e)
{
status = e.getMessage();
}
return f.toString();
}
EDIT: I have a process that should terminate after waiting for x amount of time for a resource to become available, instead of waiting indefinitely.