-
Notifications
You must be signed in to change notification settings - Fork 160
/
MyThreadPool.java
84 lines (70 loc) · 1.97 KB
/
MyThreadPool.java
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
package com.example.concurrency.features.threadPool;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* 描述:
* 简化的线程池,仅用来说明工作原理
*
* @author zed
* @since 2019-06-18 3:10 PM
*/
public class MyThreadPool{
/**
* 利用阻塞队列实现生产者 - 消费者模式
*/
private BlockingQueue<Runnable> workQueue;
/**
* 保存内部工作线程
*/
private List<WorkerThread> threads = new ArrayList<>();
// 构造方法
public MyThreadPool(int poolSize, BlockingQueue<Runnable> workQueue){
this.workQueue = workQueue;
// 创建工作线程
for(int idx=0; idx<poolSize; idx++){
WorkerThread work = new WorkerThread();
work.start();
threads.add(work);
}
}
/**
* 提交任务
* @param command command
*/
public void execute(Runnable command){
try{
workQueue.put(command);
}catch (InterruptedException e){
Thread.currentThread().interrupt();
}
}
/**
* 工作线程负责消费任务,并执行任务
*/
class WorkerThread extends Thread{
@Override
public void run() {
// 循环取任务并执行
while(true){
try{
Runnable task = workQueue.take();
task.run();
}catch (InterruptedException e){
Thread.currentThread().interrupt();
}
}
}
}
public static void main(String[] args) {
// 创建有界阻塞队列
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(2);
// 创建线程池
MyThreadPool pool = new MyThreadPool(10, workQueue);
// 提交任务
pool.execute(()->{
System.out.println("hello");
});
}
}