ThreadPoolExecutor的使用和理解

ThreadPoolExecutor的使用和理解

corePoolSize,//核心线程数,当线程数超过workQueue阻塞队列数,并且核心数线程已满,会创建新的线程直到达到最大线程
maximumPoolSize,//最大线程数,
keepAliveTime,//空闲线程等待时间,时间到结束释放
unit,//等待时间单位
workQueue, //线程阻塞队列
threadFactory,//线程生产工厂
handler);//当线程数大于maximumPoolSize+workQueue的时候会执行RejectedExecutionHandler

ThreadFactory是一个线程工厂,负责生产线程的

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadPollTest {
static ThreadPoolExecutor executor;
public static void main(String[] args) throws InterruptedException, IOException {
int corePoolSize = 2;
int maximumPoolSize = 4;
long keepAliveTime = 60;
TimeUnit unit = TimeUnit.SECONDS;
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(10);
ThreadFactory threadFactory = new NameTreadFactory();
RejectedExecutionHandler handler = new MyIgnorePolicy();
executor = new ThreadPoolExecutor(
corePoolSize,//核心线程数,当线程数超过workQueue阻塞队列数,并且核心数线程已满,会创建新的线程直到达到最大线程
maximumPoolSize,//最大线程数,
keepAliveTime,//空闲线程等待时间,时间到结束释放
unit,//等待时间单位
workQueue, //线程阻塞队列
threadFactory,//线程生产工厂
handler);//当线程数大于maximumPoolSize+workQueue的时候会执行RejectedExecutionHandler
executor.prestartAllCoreThreads(); // 预启动所有核心线程

for (int i = 1; i <= 16; i++) {
MyTask task = new MyTask(String.valueOf(i));
executor.execute(task);
}

}
//线程工厂
static class NameTreadFactory implements ThreadFactory {

private final AtomicInteger mThreadNum = new AtomicInteger(1);

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "my-thread-" + mThreadNum.getAndIncrement());
System.out.println(t.getName() + " has been created");
return t;
}
}
//线程数大于maximumPoolSize+workQueue的时候会执行RejectedExecutionHandler
public static class MyIgnorePolicy implements RejectedExecutionHandler {

public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
doLog(r, e);
}

private void doLog(Runnable r, ThreadPoolExecutor e) {
System.err.println( r.toString() + " rejected");
}
}
//例子执行任务
static class MyTask implements Runnable {
private String name;

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

@Override
public void run() {
try {
System.out.println(this.toString() + " is running!activityCount:"+executor.getActiveCount()
+",getCompletedTaskCount:"+executor.getCompletedTaskCount()
+",getTaskCount:"+executor.getTaskCount()
+",getQueue().size:"+executor.getQueue().size()
);
Thread.sleep(3000); //让任务执行慢点
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public String getName() {
return name;
}

@Override
public String toString() {
return "MyTask [name=" + name + "]";
}
}
}