Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 495
Alfred Avatar asked Jan 20 '23 22:01

Alfred


2 Answers

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.

like image 109
Eyal Schneider Avatar answered Jan 30 '23 19:01

Eyal Schneider


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

like image 42
Boris Pavlović Avatar answered Jan 30 '23 19:01

Boris Pavlović