Alfred
Alfred

Reputation: 1841

Does the thread continue running when Future.get(timeout) timeouts

As the title showed, If Future.get(timeout) timeout, does the thread continue running,

ExecutorService executor = Executors.newFixedThreadPool(n);
Callable<Object> task = new Callable<Object>() {
  public Object call() {
       //...
 }
}
Future<Object> future = executor.submit(task);
try {
  Object result = future.get(5, TimeUnit.SECONDS); 
} catch (TimeoutException ex) {
  // handle the timeout
}

If the thread continue to run and get blocked due to some IO, etc, then when the threadpool get full, not new task can be sumitted, which means the trheadpool gets stuck, since all the threads in the pool are blocked, right?

Upvotes: 5

Views: 5937

Answers (2)

Eyal Schneider
Eyal Schneider

Reputation: 22446

The call to future.get(..) will block the thread running it for up to 5 seconds. The task executed by the thread pool will be unaffected, and will continue running until a graceful termination / exception / interruption.

Regarding the submission of new tasks when the thread pool is in full capacity, in your case the tasks WILL be submitted (releasing the submitter thread immediately), but will wait in the thread pool queue for execution. The API documentation of Executors.newFixedThreadPool(..) specifies this clearly.

Upvotes: 7

Boris Pavlović
Boris Pavlović

Reputation: 64632

Right, underlaying thread will be live until IO thrown an exception or ended.

Upvotes: 5

Related Questions