**我们平时使用线程池是方式:**
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
Executors类,提供了一系列工厂方法用于创建线程池,返回的线程池都实现了ExecutorService接口。里面全是一些静态方法,返回的是实现了ExecutorService接口
**接下来看看ExecutorService:**
ExecutorService:是一个比Executor使用更广泛的子类接口,其提供了生命周期管理的方法,返回 Future 对象,以及可跟踪一个或多个异步任务执行状况返回Future的方法;可以调用ExecutorService的shutdown()方法来平滑地关闭 ExecutorService,调用该方法后,将导致ExecutorService停止接受任何新的任务且等待已经提交的任务执行完成(已经提交的任务会分两类:一类是已经在执行的,另一类是还没有开始执行的),当所有已经提交的任务执行完毕后将会关闭ExecutorService。因此我们一般用该接口来实现和管理多线程。
Executor的定义如下:
Executor:一个接口,其定义了一个接收Runnable对象的方法executor,其方法签名为executor(Runnable command),该方法接收一个Runable实例,它用来执行一个任务,任务即一个实现了Runnable接口的类,一般来说,Runnable任务开辟在新线程中的使用方法为:new Thread(new RunnableTask())).start(),但在Executor中,可以使用Executor而不用显示地创建线程:executor.execute(new RunnableTask()); // 异步执行
Executor框架包括:线程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等。
ThreadPoolExecutor
java.uitl.concurrent.ThreadPoolExecutor类是线程池中最核心的一个类,Executors中新建四种线程池的方法都是返回的一个ThreadPoolExecutor.
在ThreadPoolExecutor类中提供了四个构造方法:
-
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); }
-
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler); }
-
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), handler); }
-
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null || handler == null) throw new NullPointerException(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }
123构造器最后都是调用的最后一个构造器
参数说明:
- corePoolSize:核心池的大小,在创建了线程池后,默认情况下,线程池中没有任何线程,而是等待有任务到来才创建线程去执行任务,当有任务来之后,会创建一个线程去处理任务,当线程数量大于corePoolSize时,新建的线程就会加入到缓存中等待执行。如果调用了prestartAllCoreThreads()或者prestartCoreThread()方法,这两个方法是的作用是预创建线程的意思,即在没有任务到来的时候就会创建corPoolSize个线程或者一个线程。
- maximumPoolSize:线程池最大线程数,他表示在线程池中最多能创建多少个线程
- keepAliveTime:表示线程池没有任务执行时最多保持多久时间会终止。默认情况下,只有当线程池中的线程数大于corePoolSize时,keepAliveTime才会起作用
- unit:参数KeepAliveTime的时间单位
- workQueue:一个阻塞队列,用来存储等待执行的任务
- ThreadFactory:线程工程,主要用来创建线程
- handler:表示拒绝处理任务是的策略
接下来看看ThreadPoolService继承的AbstractExecutorService
AbstractExecutorService是一个抽象类,它实现了ExecutorService接口。