目录页:https://blog.csdn.net/u011294519/article/details/88367808
1.类层级关系及主要方法
1.1.类层级关系
- Executor:顶层接口
- ExecutorService:继承Executor接口,定义管理方法
- AbstractExecutorService:抽象类,实现ExecutorService接口作为骨架类
- ThreadPoolExecutor:线程池的具体方法,继承AbstractExecutorService类
2.主要方法
2.1.构造方法
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
corePoolSize:线程池中的核心线程数
maximumPoolSize:线程池中拥有的最大线程数
unit:空闲线程的存活时间,注意,核心线程数是否减少根据另一个参数allowCoreThreadTimeOut来决定
workQueue:阻塞队列,当核心线程数达到最大值时仍有任务进入则放入阻塞队列等待核心线程空闲时来此获取任务,主要使用的阻塞队列有以下4种:
ArrayBlockingQueue | 由数组组成的有界队阻塞队列 |
LinkedBlockingQueue | 链表结构的有界阻塞队列 |
PriorityBlockingQueue | 支持优先级排列的无界阻塞队列 |
LinkedTransferQueue | 链表结构的无界阻塞队列 |
handler:表示当workQueue已满,且池中的线程数达到maximumPoolSize时,线程池拒绝添加新任务时采取的策略。主要有以下4种
ThreadPoolExecutor.AbortPolicy() | 抛出RejectedExecutionException异常 |
ThreadPoolExecutor.CallerRunsPolicy() | 由向线程池提交任务的线程来执行该任务 |
ThreadPoolExecutor.DiscardOldestPolicy() | 抛弃最旧的任务(最先提交而没有得到执行的任务 |
ThreadPoolExecutor.DiscardPolicy() | 抛弃当前的任务 |
下面就详细的将下参数间的关
下面就详细的将下参数间的关系
- 如果没有空闲的线程执行新任务且当前运行的线程数少于corePoolSize,则添加新的线程执行该任务。(线程池不区分哪个线程是核心线程,只关注线程数量)
- 如果没有空闲的线程且线程数等于corePoolSize设置的值,而且阻塞队列未满,则将任务入队列,而不是添加新的线程。
- 如果没有空闲的线程执行新任务且阻塞队列已满同时池中的线程数小于maximumPoolSize,则创建新的线程执行任务,所以若阻塞队列使用的是无界队列则线程池中执行任务的线程数永远等于corePoolSize设置的值。
- 如果没有空闲的线程执行新任务且阻塞队列已满同时池中的线程数等于maximumPoolSize,则根据构造函数中的handler指定的策略来拒绝新的任务。
2.2.execute与submit方法
execute方法与submit方法都是用于提交任务到线程池,但是submit方法可以将callable线程的返回值取出。
示例代码如下:
package com.concurrent.coline.part13;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;
public class ThreadMethodDemo {
private static class MyWork implements Runnable {
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static class MyCallable implements Callable {
@Override
public String call() throws Exception {
return "Callable: " + Thread.currentThread().getName();
}
}
public static void main(String[] arg0) throws ExecutionException, InterruptedException {
ThreadFactory demoThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("demo-thread-%d").build();
ExecutorService threadPool = new ThreadPoolExecutor(2, 4
, 10L, TimeUnit.SECONDS
, new ArrayBlockingQueue<>(10), demoThreadFactory, new ThreadPoolExecutor.AbortPolicy());
for (int i = 0; i < 10; i++) {
MyWork work = new MyWork();
threadPool.execute(work);
}
for (int i = 0; i < 10; i++) {
MyCallable myCallable = new MyCallable();
Future result = threadPool.submit(myCallable);
System.out.println(result.get());
}
threadPool.shutdown();
}
}
代码位置在thread-pool的ThreadMethodDemo
2.3.beforeExecute与afterExecute方法
beforeExecute方法和afterExecute方法用于在任务执行之前和任务执行之后做一些自己的操作,类似于Spring的AOP。
示例代码如下:
package com.concurrent.coline.part13;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;
public class ThreadPoolTest {
private static class MyWork implements Runnable {
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static class MyCallable implements Callable {
@Override
public String call() throws Exception {
return "Callable: " + Thread.currentThread().getName();
}
}
public static void main(String[] arg0) throws ExecutionException, InterruptedException {
ThreadFactory demoThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("demo-thread-%d").build();
ExecutorService threadPool = new ThreadPoolExecutor(2, 4
, 10L, TimeUnit.SECONDS
, new ArrayBlockingQueue<>(10), demoThreadFactory, new ThreadPoolExecutor.AbortPolicy()) {
@Override
protected void beforeExecute(Thread t, Runnable r) {
System.out.println("before:" + Thread.currentThread().getName());
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
System.out.println("after:" + Thread.currentThread().getName());
}
@Override
protected void terminated() {
System.out.println("线程结束");
}
};
for (int i = 0; i < 10; i++) {
MyWork work = new MyWork();
threadPool.execute(work);
}
for (int i = 0; i < 10; i++) {
MyCallable myCallable = new MyCallable();
Future result = threadPool.submit(myCallable);
System.out.println(result.get());
}
threadPool.shutdown();
}
}
代码位置在thread-pool模块的ThreadPoolTest
2.4.shutdown与shutdownNow方法
shutdown方法与shutdownNow方法都是用于关闭线程池,区别如下:
shutdown方法首先将线程池状态修改为SHUTDOWN,然后调用线程interrupt()方法将空闲线程的中断标准位设置为中断,使用该方式关闭线程池相对优雅。
shutdownNow方法会将线程池中所有线程的中断标志位设置为中断,不管任务是否执行完成。
对于线程状态有以下5种:
RUNNING | 允许提交并处理任务 |
SHUTDOWN | 不允许提交新的任务,但是会处理完已提交的任务 |
STOP | 不允许提交新的任务,也不会处理阻塞队列中未执行的任务,并设置正在执行的线程的中断标志位 |
TIDYING | 所有任务执行完毕,池中工作的线程数为0,等待执行terminated()方法 |
TERMINATED | terminated()方法执行完毕 |