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();
        }
    }
}
  • 复现代码 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();
        }
    }
}

参考:https://blog.csdn.net/xiaolyuh123/article/details/85023269

发布了441 篇原创文章 · 获赞 110 · 访问量 57万+

猜你喜欢

转载自blog.csdn.net/qq_26437925/article/details/98790710
今日推荐