首页  

线程池实例之提交返回类型不同的任务     所属分类 java 浏览量 755
pool.submit 返回 future , future.get 获取结果
构建 FutureTask 提交到线程池 ,然后通过 FutureTask.get 获取结果
FutureTask 实现了RunnableFuture接口 
RunnableFuture继承了Runnable和Future接口
既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值



import java.time.LocalDateTime;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
import java.util.concurrent.TimeUnit;

public class MultiTypeValueFuture {

    public static void main(String[] args) throws Exception {
        int coreSize = 1;
        int maxSize = 3;
        BlockingQueue<Runnable> workQueue = new SynchronousQueue<Runnable>();

        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        RejectedExecutionHandler rejectHandler = new AbortPolicy();
        ThreadPoolExecutor pool = new ThreadPoolExecutor(coreSize, maxSize, 60, TimeUnit.SECONDS, workQueue,
                threadFactory, rejectHandler);

        Future<Integer> intResultFuture = pool.submit(new IntResultTask());
        System.out.println("IntResultTask submit done," + LocalDateTime.now().toString());
        Future<String> stringResultFuture = pool.submit(new StringResultTask());
        System.out.println("StringResultTask submit done," + LocalDateTime.now().toString());

        // 构建 FutureTask 提交
        FutureTask<Long> longResultFutureTask = new FutureTask<>(new LongResultTask());
        pool.submit(longResultFutureTask);
        System.out.println("LongResultTask submit done," + LocalDateTime.now().toString());

        System.out.println();

        Integer intResult = intResultFuture.get();
        System.out.println("intResult=" + intResult + "," + LocalDateTime.now().toString());

        String stringResult = stringResultFuture.get();
        System.out.println("stringResult=" + stringResult + "," + LocalDateTime.now().toString());

        Long longResult = longResultFutureTask.get();
        System.out.println("longResult=" + longResult + "," + LocalDateTime.now().toString());

    }

    private static class IntResultTask implements Callable<Integer> {
        public Integer call() throws Exception {
            Thread.sleep(1000);
            System.out.println("IntResultTask done," + LocalDateTime.now().toString());
            return 1;
        }
    }

    private static class StringResultTask implements Callable<String> {
        public String call() throws Exception {
            Thread.sleep(1500);
            System.out.println("StringResultTask done," + LocalDateTime.now().toString());
            return LocalDateTime.now().toString();
        }
    }

    private static class LongResultTask implements Callable<Long> {
        public Long call() throws Exception {
            Thread.sleep(500);
            System.out.println("LongResultTask done," + LocalDateTime.now().toString());
            return System.currentTimeMillis();
        }
    }
}



IntResultTask submit done,2021-10-30T11:35:55.998 StringResultTask submit done,2021-10-30T11:35:56 LongResultTask submit done,2021-10-30T11:35:56.002 LongResultTask done,2021-10-30T11:35:56.502 IntResultTask done,2021-10-30T11:35:56.802 intResult=1,2021-10-30T11:35:56.802 StringResultTask done,2021-10-30T11:35:57.503 stringResult=2021-10-30T11:35:57.503,2021-10-30T11:35:57.503 longResult=1635564956503,2021-10-30T11:35:57.504
完整代码 https://gitee.com/dyyx/hellocode/blob/master/src/dyyx/conc/MultiTypeValueFuture.java
Runnable Callable Future FutureTask

上一篇     下一篇
Collectors groupingBy 用法及性能

能源化工ETF 159981

马斯洛需求层次理论

FutureTask简介

框架效应

正态分布和幂律分布