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

As you can see from the header, If Timetime (timeout) timeout, the stream continues to work,

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 a thread continues to run and is blocked due to some I / O, etc., then when the thread is empty it cannot be skipped a new task, which means that trheadpool is stuck because all threads in the pool are blocked, isn’t it ?

+3
source share
2 answers

Calling the function future.get (..) blocks the thread that runs it for 5 seconds. The task performed by the thread pool will not be affected and will continue to run until graceful completion / exception / interruption.

, , ( ), . Executors.newFixedThreadPool(..) .

+6

, , IO .

+3

Source: https://habr.com/ru/post/1779148/


All Articles