自定义线程池+自定义拒绝策略+异步获取线程执行结果(Future)

package com.calvin.currency.juc;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.stream.Collectors;

/**
 * @Title ThreadPoolTest
 * @Description 自定义线程池
 * @author calvin
 * @date: 2020/3/1 2:13 PM 
 */
public class ThreadPoolTest {

    // 基于数组的阻塞队列
    private static BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>(3);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 自定义线程池
//        ExecutorService threadPool = new ThreadPoolExecutor(2, 5, 3,
//                TimeUnit.SECONDS, new LinkedBlockingQueue<>(3),
//                Executors.defaultThreadFactory(), new DiscardOldestPolicy());
        ExecutorService threadPool = new ThreadPoolExecutor(2, 5, 3,
                TimeUnit.SECONDS, blockingQueue,
                Executors.defaultThreadFactory(),
                // 自定义策略
                (r, executor) -> {
                    try {
                        if (!executor.isShutdown()) {
                            while (executor.getQueue().remainingCapacity() == 0) {
                                System.out.println("队列满啦,而且线程数量达到最大数量啦!");
                                executor.getQueue().put(r);
                            }
                        }
                    } catch (Exception e) {
                        System.out.println("塞入队列异常啦!发邮件报警给运维");
                    }
                    System.out.println("塞进队列成功!");
                });
        int sum = calculateSum(threadPool);
        System.out.println("sum = " + sum);
    }

    private static int calculateSum(ExecutorService threadPool) throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        AtomicInteger atomicInteger = new AtomicInteger(0);
        Future<Integer> future = new CompletableFuture<>();
        try {
            for (int i = 1; i <= 9; i++) {
                future = threadPool.submit(() -> {
                    System.out.println("ThreadName = " + Thread.currentThread().getName());
                }, atomicInteger.addAndGet(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
        long end = System.currentTimeMillis();
        System.out.println("总共耗时:" + (end - start) + "毫秒");
        // 异步执行,获取线程执行后的返回结果
        return future.get();
    }

}

控制台输出结果:
自定义线程池+自定义拒绝策略+异步获取线程执行结果(Future)_第1张图片

你可能感兴趣的:(综合实战,Java应用篇)