java并发编程之Executor与ThreadPoolExecutor的使用

在JDK5中提供了线程池的支持,主要的作用是支持高并发的访问处理,并且可以将线程对象进行复用,核心原理即创建了一个运行效率比较优异的“线程池ThreadPool”,在池中支持线程对象管理,包括创建与销毁,使用池中只需要执行具体的任务即可,线程对象的吃力都在池中被封装了。

Executor接口介绍

4.2 使用Executors工厂类创建线程池

接口Executor仅仅是一种规范,是一种声明,是一种定义,并没有实现任何的功能,所以在大多数的情况下,需要使用接口的实现类来完成指定的功能,就是Executor的实现类,但ThreadPoolExecutor在使用上并不是那么方便,在实例化时需要传入很多个参数,还要考虑线程的并发数等与线程池运行效率有关的参数,所以官方建议使用Executors工厂类来创建线程池对象。

4.2.1 使用newCachedThreadPool()方法创建无界线程池

使用Executors类的newCachedThreadPool()方法创建的是无界线程池,可以进行线程自动回收。所谓的“无界线程池”就是池中存放线程个数是理论上的Integer.MAX_VALUE最大值。

ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(new Runnable(){
  public void run(){
 try{
   ........
 }catch(InterruptedException e){
   e.printStackTrace();
 }
}
});

4.2.2 使用newCachedThreadPool(ThreadFactory)

无界线程池中的Thread类还可以由程序员自己定制,方法newCachedThreadPool(ThreadFactory)就是解决这个问题的。

public class MyThreadFactory implements ThreadFactory{
  public Thread newThread(Runnable r){
  Thread thread = new Thread(r);
  thread.setName("定制池中的线程对象的名词"+Math.random());
  return thread;
 }
}

4.2.3 使用newFixedThreadPool(int)方法创建有界线程池

方法newFixedThreadPool(int)创建的是有界线程池,也就是池中的线程个数可以指定最大数量。

ExecutorService executorService = Executors.newFixedThreadPool(3);
使用有界线程池后线程池中的最多线程个数是可控的。
还有方法 newFixedThreadPool(int ,ThreadFactory)定制线程工厂

4.2.4 使用newSingleThreadExecutor()方法 创建单一线程

使用newSingleThreadExecutor()方法可以创建单一线程池,单一线程池可以实现以队列的方式来执行任务。

ExecutorService executorService = Executors.newSingleThreadExecutor();
还有 newSingleThreadExecutor(ThreadFactory)

4.3 使用ThreadPoolExecutor的使用

类ThreadPoolExecutor可以非常方便地创建线程池对象,而不需要程序员设计大量的new实例化Thread相关的代码。
上面newSingleThreadExecutor()方法内部其实是实例化了1个ThreadPoolExecutor类的实例,源代码如下:

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

4.3.1 使用ThreadPoolExecutor的构造方法

ThreadPoolExecutor(int corePoolSise,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue)

corePoolSize:池中所保存的线程数,包括空闲线程,也就是核心池的大小。
maximumPoolSize:池中允许的最大线程数
keepAliveTime:当线程数量大于corePoolSize值时,在没有超过指定的时间内是不会从线程池中将空闲线程删除的,如果超过此时间单位,则删除
unit:keepAliveTime参数的时间单位
workQueue:执行前用于保持任务的队列。此队列仅保持由execute方法提交到Runnable任务。
4.3.2 如果线程的数量<=corePoolSize

ThreadPoolExecutor executor = new ThreadPoolExecutor(7,8,5,TimeUnit.SECONDS,new 
LinkedBlockingDeque<Runnable>());
executor.getCorePoolSize();//获得核心池的大小
executor.getPoolSize();//获得当前池中线程的数量
executor.getQueue().size();//获得队列中线程的数量

如果线程的数量小于corePoolSize,线程超过指定时间也不清除,也不放入扩展队列Queue中,其他参数功能忽略

4.3.3 如果线程数量>corePoolSize并且<=maximumPoolSize

如果线程数量>corePoolSize并且<=maximumPoolSize,参数keepAliveTime与参数maximumPoolSize被忽略,并且把超过corePoolSize的线程数量放入LinkedBlockingDeque中等待被执行。
BlockingQueue只是一个接口,常用的实现类有LinkedBlockingQueue和ArrayBlockingQueue.用LinkedBlockingQueue的好处在于没有大小限制,有显示队列容量非常大,所以执行execute()不会被抛出异常,而线程池中运行的线程数量也永远不会超过corePoolSize值,以为其他多余的线程被放入LinkedBlockingQueue队列中,keepAliveTime参数也就没意义了。

如果用的是 SynchronousQueue,则参数keepAliveTime与参数maximumPoolSize有效,超过的线程数不放入队列中,多余线程执行完任务在,在指定的时间后删除。

4.3.4 当创建线程数量>maximumPoolSize

如果线程数量>maximumPoolSize,则maximumPoolSize与keepAliveTime参数忽略,并把超过corePoolSize的线程数量放入LinkedBlockingQueue队列中等待被执行。

如果用的是SynchronousQueue, 则处理maximumPoolSize个线程,超过的报异常。

4.3.5 参数keepAliveTime为0时

当线程数量大于corePoolSize值时,在没有超过指定的时间内是不从线程池中将空闲线程删除的,如果超过此时间单位,则删除,如果为0则任务执行完毕后立即从队列中删除。

4.3.6 使用shutdown()和shutdownNow()与返回值

方法shutdown()的作用是使当前未执行完的线程继续执行,则不再添加新的任务Task,还有shutdown()方法不会阻塞,调用shutdown()方法后,主线程main就马上结束了,而线程池会继续运行直到所有任务执行完才会停止。如果不调用shutdown()方法,那么线程池会一直保持下去,以便随时执行被添加的新Task任务。

方法shutdwonNow()的作用是中断所有的任务Task,并且抛出InterruptedException异常,前提是在Runnable中使用if(Thread.currentThread().isInterrupted()==true)语句来判断当前线程的中断状态,而未执行的线程不再执行,也就是从执行队列中清除。如果没有if(Thread.currentThread().isInterrupted()==true)语句及抛出异常的代码,则池中正在运行的线程直到执行完毕,而未执行的线程不再执行,也从执行队列中清除。

4.3.7 方法isShutdown() isTerminating() isTerminated()

方法isShutdown()的作用是判断线程池是否已经关闭
如果正在执行的程序处于shutdown或shutdownNow之后处于正在终止但尚未完全终止的过程中调用方法isTerminating()则返回true。此方法比喻成,门是否正在关闭。门彻底关闭时,线程池也就关闭了。
如果线程池关闭后,也就是所有任务都已完成,则方法isTerminated()返回true.此方法可以比喻成,门已经关闭。

4.3.6 方法awaitTermination(long timeout,TimeUnit unit)

方法awaitTermination(long timeout,TimeUnit unit)的作用就是查看在指定的时间之间,线程池是否已经终止工作,也就是最多,也就是最多等待多少时间后去判断线程池是否已经终止工作。

方法awaitTermination()被执行时,如果池中有任务在被执行时,则调用awaitTermination()方法出现阻塞,等待指定的时间,如果没有任务时则不再阻塞。

4.3.7 方法set/getRejectedExecutionHandler()

方法setRejectedExecutionHandler()和getRejectedExecutionHandler()的作用是可以处理任务被拒绝执行时的行为。

4.3.8 方法allowsCoreThreadTimeOut(boolean value)

方法allowsCoreThreadTimeOut()和allowsCoreThreadTimeOut(boolean value)的作用是配置核心线程是否有超时的效果

4.3.9 方法prestartCoreThread()和prestartAllCoreThreads()

方法prestartCoreThread()每调用一次就创建一个核心线程,返回值为boolean,含义是是否启动了
方法prestartAllCoreThreads()的作用是启动全部核心线程,返回值是启动核心线程的数量。

4.3.10 方法getCompletedTaskCount()

方法getCompletedTaskCount()的作用是取得已经执行完成的任务数。

4.3.11 常见3种队列结合max值的因果效果

在使用ThreadPoolExecutor线程池的过程中,常使用3种队列ArrayBlockingQueue、LinkedBlockingDeque、SynchronousQueue

LinkedBlockingDequeArrayBlockingQueue效果一样

LinkedBlockingDeque linked = new LinkedBlockingDeque<Runnable>(2);
ThreadPoolExecutor pool = new ThreadPoolExecutor(2,3,5,TimeUnit.SECONDS,linked);
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
//队列中放入两个任务,执行3个任务
LinkedBlockingDeque linked = new LinkedBlockingDeque<Runnable>(2);
ThreadPoolExecutor pool = new ThreadPoolExecutor(2,3,5,TimeUnit.SECONDS,linked);
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
//队列中放入两个任务,执行3个任务,拒绝一个任务

SynchronousQueue

SynchronousgQueue linked = new SynchronousQueue<Runnable>(2);
ThreadPoolExecutor pool = new ThreadPoolExecutor(2,3,5,TimeUnit.SECONDS,linked);
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
//直接执行3个任务
SynchronousgQueue linked = new SynchronousQueue<Runnable>(2);
ThreadPoolExecutor pool = new ThreadPoolExecutor(2,3,5,TimeUnit.SECONDS,linked);
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
//直接执行3个任务,有一个任务被拒绝

4.3.12 线程池ThreadPoolExecutor的拒绝策略

线程池中的资源全部被占用的时候,对新添加的Task任务有不同的处理策略,在默认的情况下,ThreadPPoolExecutor类中有四种不同的处理方式:

AbortPolicy: 当任务添加到线程池中被拒绝时,他将抛出RejectedExecutionException异常。
CallerRunsPolicy: 当任务添加到线程池中被拒绝时,会使用调用线程池的Thread线程对象处理被拒绝的任务
DiscardOldestPolicy: 当任务添加到线程池中被拒绝时,线程池会放弃等待队列中最旧的未处理任务,然后将被拒绝的任务添加到等待队列中。
DiscardPolicy: 当任务添加到线程池中被拒绝时,线程池将丢弃被拒绝的任务。

4.3.13 方法afterExecute()和 beforeExecute()

在线程池ThreadPoolExecutor类中重写这两个方法可以对线程池中执行的线程对象实现监控。

4.3.14 方法remove(Runnable)的使用

方法remove(Runnable)可以删除尚未被执行的Runnable任务

4.3.15 多个get方法测试

1.方法getActiveCount() 取得有多少个线程正在执行任务
2.方法getCompletedTaskCount() 取得有多少个线程已经执行完任务了
3.方法getTaskCount() 取得有多少个任务发送给了线程池

4.3.16 线程池ThreadPoolExecutor与Runnable执行为乱序特性

接口Runnable在ThreadPoolExecutor的队列中是按顺序取出,执行却是乱序的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值