CompletableFuture future.get 遇到 java.util.concurrent.TimeoutException

2019-08-07 工作中遇到的问题

事件情况描述: 有个线程池满了,然后新的任务使用CompletableFuture.supplyAsync执行,用future.get(1, TimeUnit.SECONDS)) 去获取(即使把get事件设置的很大)时报错java.util.concurrent.TimeoutException

报错java.util.concurrent.TimeoutException觉得很奇怪;随后debug发现CompletableFuture.supplyAsync的执行任务压根儿没执行;最后是调整了线程池解决

线程池满了,线程池被耗完了: 1,同时运行当任务太多,确实没有多的了;2,有耗时的任务,即使用个异步任务,也无法执行

  • 复现代码 1
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author mubi
 * @Date 2019/8/5 14:27
 */
public class Test{

    static class MyThreadFactory implements ThreadFactory {

        private AtomicInteger count = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            String threadName = "MyThread" + count.addAndGet(1);
            // System.out.println(threadName);
            t.setName(threadName);
            return t;
        }
    }

    static String printHello(){
        return "hello";
    }

    public static void main(String[] args) {
        int corePoolSize = 1;
        int maximumPoolSize = 2;
        int keepAliveTime = 60 * 1;
        TimeUnit timeUnit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(2);
        RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardPolicy();
//        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
        ThreadFactory threadFactory = new MyThreadFactory();
        // 线程池构造
        ThreadPoolExecutor taskExecutor = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                timeUnit,
                workQueue,
                threadFactory,
                handler);

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                // 耗时操作,会沾满线程池
//                taskExecutor.execute(() -> {
//                    try {
//                        TimeUnit.SECONDS.sleep(10);
//                    }catch (Exception e){
//                        e.printStackTrace();
//                    }
//                });

                // 第二步不耗时的操作,但是get的时候会报TimeoutException
                CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(Test::printHello, taskExecutor);
                try {
                    System.out.println(Thread.currentThread().getName() + "::value1 " + future1.get(1, TimeUnit.SECONDS));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
        try {
            TimeUnit.SECONDS.sleep(5000);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

某次输出如下

Thread-9::value1 hello
Thread-3::value1 hello
Thread-2::value1 hello
Thread-8::value1 hello
java.util.concurrent.TimeoutException
	at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
	at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
	at Main.lambda$main$1(Main.java:539)
	at java.lang.Thread.run(Thread.java:748)
java.util.concurrent.TimeoutException
	at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
	at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
	at Main.lambda$main$1(Main.java:539)
	at java.lang.Thread.run(Thread.java:748)
java.util.concurrent.TimeoutException
	at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
	at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
	at Main.lambda$main$1(Main.java:539)
	at java.lang.Thread.run(Thread.java:748)
java.util.concurrent.TimeoutException
	at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
	at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
	at Main.lambda$main$1(Main.java:539)
	at java.lang.Thread.run(Thread.java:748)
java.util.concurrent.TimeoutException
	at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
	at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
	at Main.lambda$main$1(Main.java:539)
	at java.lang.Thread.run(Thread.java:748)
java.util.concurrent.TimeoutException
	at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
	at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
	at Main.lambda$main$1(Main.java:539)
	at java.lang.Thread.run(Thread.java:748)
  • 复现代码 2
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author mubi
 * @Date 2019/8/5 14:27
 */
public class Test{

    static class MyThreadFactory implements ThreadFactory {

        private AtomicInteger count = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            String threadName = "MyThread" + count.addAndGet(1);
            System.out.println(threadName);
            t.setName(threadName);
            return t;
        }
    }

    static String printHello(){
        return "hello";
    }

    public static void main(String[] args) {
        int corePoolSize = 1;
        int maximumPoolSize = 2;
        int keepAliveTime = 60 * 1;
        TimeUnit timeUnit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(2);
        RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardPolicy();
//        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
        ThreadFactory threadFactory = new MyThreadFactory();
        // 线程池构造
        ThreadPoolExecutor taskExecutor = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                timeUnit,
                workQueue,
                threadFactory,
                handler);

        for (int i = 1; i < 3; i++) {
            new Thread(() -> {
                // 耗时操作,会沾满线程池
                taskExecutor.execute(() -> {
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                });

                // 第二步不耗时的操作,但是get的时候会报TimeoutException
                CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(Test::printHello, taskExecutor);
                try {
                    System.out.println(Thread.currentThread().getName() + "::value1 " + future1.get(1, TimeUnit.SECONDS));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
        try {
            TimeUnit.SECONDS.sleep(5000);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
  • 输出如下
Thread-0::value1 hello
java.util.concurrent.TimeoutException
	at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1771)
	at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
	at Main.lambda$main$2(Main.java:539)
	at java.lang.Thread.run(Thread.java:748)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值