yanze chen created IOTDB-5471: --------------------------------- Summary: Add isDone() check in WrappedThreadPoolExecutor#afterExecute Key: IOTDB-5471 URL: https://issues.apache.org/jira/browse/IOTDB-5471 Project: Apache IoTDB Issue Type: Improvement Reporter: yanze chen Assignee: yanze chen
I want to use the thread pool created by the IoTDBThreadPoolFactory in CompleteFuture. However, I have found that this can cause problems with threads not being released. Here is the example code. {code:java} import org.apache.iotdb.commons.concurrent.IoTDBThreadPoolFactory; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; public class Tests { public static void main(String[] args) { ExecutorService flushTaskExecutor1 = IoTDBThreadPoolFactory.newFixedThreadPool(2, "1"); CompletableFuture<Void>[] completableFutures = new CompletableFuture[5]; for (int j = 0; j < 5; j++) { completableFutures[j] = CompletableFuture.supplyAsync( () -> { System.out.println("run"); return null; }, flushTaskExecutor1); } CompletableFuture.allOf(completableFutures).join(); System.out.println("done"); flushTaskExecutor1.shutdown(); } } {code} Only two “run” are printed, whereas the expectation is that 5 “run” should be printed. if I change the thread creation code to {code:java} ExecutorService flushTaskExecutor1 = Executors.newFixedThreadPool(2); {code} , it works as expected. Using the thread dump, I found that the thread was blocking at WrappedThreadPoolExecutor#afterExecute after execution. This was due to a problem with the implementation of CompleteFuture#AsyncRun. One solution I found in [https://stackoverflow.com/questions/2248131/handling-exceptions-from-java-executorservice-tasks] is to add the isDone check before invoking get(). The {{isDone()}} check is necessary in certain cases to avoid blocking. See [bugs.openjdk.org/browse/JDK-8071638|https://bugs.openjdk.org/browse/JDK-8071638] and [bugs.openjdk.org/browse/JDK-7146994|https://bugs.openjdk.org/browse/JDK-7146994] for details. -- This message was sent by Atlassian Jira (v8.20.10#820010)