java 线程池(Executors、ExecutorService、ScheduledExecutorService)和(Callable、Future、CyclicBarrier)实践篇

一、FixedThreadPool、CachedThreadPool、SingleThreadExecutor、ScheduledThreadPool 简单使用(不推荐)

public void fixedThreadPool1(){
        int size = 5;
        ExecutorService threadPool = Executors.newFixedThreadPool(size);
//        ExecutorService threadPool = Executors.newCachedThreadPool(); //缓存,同上
//        ExecutorService threadPool = Executors.newSingleThreadExecutor(); //单线程
//        ExecutorService threadPool = Executors.newScheduledThreadPool(size); //
        while (size>0) {
            threadPool.execute(new MyRunnable());
            size--;
        }
        threadPool.shutdown();
    }
class MyRunnable implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"运行了");
    }
}

不推荐原因:
1)newFixedThreadPool和newSingleThreadExecutor:
主要问题是堆积的请求处理队列可能会耗费非常大的内存,甚至OOM。
2)newCachedThreadPool和newScheduledThreadPool:
主要问题是线程数最大数是Integer.MAX_VALUE,可能会创建数量非常多的线程,甚至OOM。

二、ExecutorService、ThreadPoolExecutor实例化用法(阿里巴巴规范)【推荐】

原理:使用第三方线程工厂,com.google.guava包

<dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>27.0.1-jre</version>
</dependency>
public void alibabaThreadPool(){
        //线程池核心池的大小
        int corePoolSize = 5;
        //线程池的最大线程数
        int maximumPoolSize = 200;
        //当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间
        long keepAliveTime = 0L;
        //keepAliveTime 的时间单位
        TimeUnit unit = TimeUnit.MILLISECONDS;
        //用来储存等待执行任务的队列
        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(1024);
        //线程工厂,使用com.google.guava包
        ThreadFactory threadFactory  = new ThreadFactoryBuilder().setNameFormat("my-pool-%d").build();
        //拒绝策略
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
        ExecutorService threadPool = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue,threadFactory,handler);
        //这里为了备注各参数用途,就没有像下面优雅编写
        /*ExecutorService threadPool = new ThreadPoolExecutor(5,200,0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(1024),
                new ThreadFactoryBuilder().setNameFormat("my-pool-%d").build(),new ThreadPoolExecutor.AbortPolicy());*/
        while (corePoolSize>0) {
            threadPool.execute(new MyRunnable());
            corePoolSize--;
        }
        threadPool.shutdown();
    }

三、Future、Callable有返回值,get()阻塞串行

原理:Future是ExecutorService 调用submit()返回的结果集(泛型),实现Callable接口,支持线程执行完返回结果。

/***
     * 有返回,串行
     * @throws Exception
     */
    public void alibabaThreadPool2() throws Exception{
        int corePoolSize = 5;
        ExecutorService threadPool = new ThreadPoolExecutor(corePoolSize,200,0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(1024),
                n
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值