java多线程之ThreadPoolExecutor

public class Demo1 {

public static void main(String[] args) {

BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(10);

RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy;

ThreadPoolExecutor pool = new ThreadPoolExecutor(3, 3, 0, TimeUnit.SECONDS, queue, handler);

for (int i = 0; i < 20; i ++){

final int temp = i; pool.execute( -> {

System.out.println("客户" + temp + "来了.......");

try {

Thread.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace;

}

});

}

pool.shutdown;

}

}



1. ThreadPoolExecutor的一个常用的构造方法


ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, 
    TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) 

参数说明:

-corePoolSize       线程池中所保存的核心线程数。线程池启动后默认是空的,只有任务来临时才会创建线程以处理请求。prestartAllCoreThreads方法可以在线程池启动后即启动所有核心线程以等待任务。

-maximumPoolSize  线程池允许创建的最大线程数。当workQueue使用无界队列时(如:LinkedBlockingQueue),则此参数无效。

-keepAliveTime      当前线程池线程总数大于核心线程数时,终止多余的空闲线程的时间。

-unit          keepAliveTime参数的时间单位。

-workQueue       工作队列,如果当前线程池达到核心线程数时(corePoolSize),且当前所有线程都处于活动状态,则将新加入的任务放到此队列中。下面仅列几个常用的:

  • ArrayBlockingQueue:  基于数组结构的有界队列,此队列按FIFO原则对任务进行排序。如果队列满了还有任务进来,则调用拒绝策略。
  • LinkedBlockingQueue:  基于链表结构的无界队列,此队列按FIFO原则对任务进行排序。因为它是无界的,根本不会满,所以采用此队列后线程池将忽略拒绝策略(handler)参数;同时还将忽略最大线程数(maximumPoolSize)等参数
  • SynchronousQueue:   直接将任务提交给线程而不是将它加入到队列,实际上此队列是空的。每个插入的操作必须等到另一个调用移除的操作;如果新任务来了线程池没有任何可用线程处理的话,则调用拒绝策略。其实要是把maximumPoolSize设置成无界(Integer.MAX_VALUE)的,加上SynchronousQueue队列,就等同于Executors.newCachedThreadPool()。
  • PriorityBlockingQueue: 具有优先级的队列的有界队列,可以自定义优先级;默认是按自然排序,可能很多场合并不合适。

-handler          拒绝策略,当线程池与workQueue队列都满了的情况下,对新加任务采取的策略。

  • AbortPolicy:           拒绝任务,抛出RejectedExecutionException异常。默认值。
  • CallerRunsPolicy:   A handler for rejected tasks that runs the rejected task directly in the calling thread of the execute method, unless the executor has been shut down, in which case the task is discarded(没太弄懂意思,看不太懂,程序模拟半天也没得出啥结论。)
  • DiscardOldestPolicy:  如果执行程序尚未关闭,则位于工作队列头部的任务将被删除,然后重试执行程序(如果再次失败,则重复此过程)。这样的结果是最后加入的任务反而有可能被执行到,先前加入的都被抛弃了。
  • DiscardPolicy:      加不进的任务都被抛弃了,同时没有异常抛出

2. 详解及示范

  1. 一个任务进来(Runnable)时,如果核心线程数(corePoolSize未达到,则直接创建线程处理该任务;如果核心线程数已经达到则该任务进入工作队列(workQueue)。如果工作队列满了(只能是有界队列),则检查最大线程数(maximumPoolSize是否达到,如果没达到则创建线程处理任务(FIFO);如果最大线程数据也达到了,则调用拒绝策略(handler)。
  2. 如果workQueue使用LinkedBlockingQueue队列,因为它是无界的,队列永远不会满,所以maximumPoolSize参数是没有意义的,同样keepAliveTimeunithandler三个参数都无意义。
  3. 如果workQueue使用ArrayBlockingQueue队列,那么小心,因为此队列是有界的,必须小心处理拒绝策略。你看人家Executors类,压根就不使用ArrayBlockingQueue队列。
  4. 正常情况下,如果使用Executors静态工厂生成的几种常用线程池能够满足要求,建议就用它们吧。自己控制所有细节挺不容易的。 
复制代码
package com.clzhang.sample.thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest3 {
    static class MyThread implements Runnable {
        private String name;

        public MyThread(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            // 做点事情
            try {
                Thread.sleep(1000);
                    
                System.out.println(name + " finished job!") ;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        // 创建线程池,为了更好的明白运行流程,增加了一些额外的代码
//        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(2);
        BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
//        BlockingQueue<Runnable> queue = new PriorityBlockingQueue<Runnable>();
//        BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>();

        
        // AbortPolicy/CallerRunsPolicy/DiscardOldestPolicy/DiscardPolicy
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 5, TimeUnit.SECONDS,
                queue, new ThreadPoolExecutor.AbortPolicy());

        // 向线程池里面扔任务
        for (int i = 0; i < 10; i++) {
            System.out.println("当前线程池大小[" + threadPool.getPoolSize() + "],当前队列大小[" + queue.size() + "]");

            threadPool.execute(new MyThread("Thread" + i));
        }
        // 关闭线程池
        threadPool.shutdown();
    }
}
复制代码

输出(采用LinkedBlockingQueue队列):

当前线程池大小[0],当前队列大小[0]
当前线程池大小[1],当前队列大小[0]
当前线程池大小[2],当前队列大小[0]
当前线程池大小[2],当前队列大小[1]
当前线程池大小[2],当前队列大小[2]
当前线程池大小[2],当前队列大小[3]
当前线程池大小[2],当前队列大小[4]
当前线程池大小[2],当前队列大小[5]
当前线程池大小[2],当前队列大小[6]
当前线程池大小[2],当前队列大小[7]
Thread1 finished job!
Thread0 finished job!
Thread3 finished job!
Thread2 finished job!
Thread4 finished job!
Thread5 finished job!
Thread6 finished job!
Thread7 finished job!
Thread8 finished job!
Thread9 finished job!

3. 回头看看Executors静态工厂方法生成线程池的源代码

Executors.newSingleThreadExecutor()

复制代码
    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }
复制代码

Executors.newFixedThreadPool()

    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

Executors.newCachedThreadPool()

    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

通过上述代码可以发现,用Executors静态工厂生成的几种常用线程池,都可以向里面插入n多任务:要么workQueue是无界的,要么maximumPoolSize是无界的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值