Java completablefuture 未清理

问题描述

Java8 提供了一个 CompletableFuture<T> 类型,但它有一些尴尬的行为,当达到 Timeout 状态时,它不会清理/取消正在运行的线程。我避免使用 Thread.sleep,因为它可能会在中断中引入意外行为(由于 try{...}catch{...} 要求)并最终使用此 PoC

    @Test
    public void futuretest() throws Exception {
        final CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            int i = 0;
            while (i < 10000) {
                long start = new Date().getTime();
                while (new Date().getTime() - start < 1000L) {
                }
                System.out.println("in loop.");
                i++;
            }
            return 123;
        });

        try {
            future.get(3,TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            System.out.println("Timeout is thrown");
        }

        future.cancel(true);

        Thread.sleep(20000L);
    }

显然代码没有做任何无意义的事情,但这是奇怪的部分。我希望在日志中看到以下内容

in loop.
in loop.
in loop.
Timeout is thrown

Process finished with ...

但我实际看到的是

in loop.
in loop.
in loop.
Timeout is thrown
in loop.
in loop.
..(repeats for 14 more seconds)

Process finished with ...

没有中断或任何事情。即使我将 future.cancel(true);future.complete(...);future.completeExceptionally(...);

交换,也会产生相同的输出

为了更进一步,我什至更改了未来使用的 Executor,但日志中也没有任何变化。

执行者 PoC 如下:

    @Test
    public void futuretest() throws Exception {
        final ExecutorService executorService = Executors.newSingleThreadExecutor();
        final CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            int i = 0;
            while (i < 10000) {
                long start = new Date().getTime();
                while (new Date().getTime() - start < 1000L) {
                }
                System.out.println("in loop.");
                i++;
            }
            return 123;
        },executorService);

        try {
            future.get(3,TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            System.out.println("Timeout is thrown");
        }

        executorService.shutdownNow();

        Thread.sleep(20000L);
    }

任何线索/想法我怎样才能实现“预期”行为,或者我完全不喜欢使用 CompletableFuture

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)