本文實例為大家分享了Java手寫線程池的實現代碼,供大家參考,具體內容如下
1.線程池是一種多線程處理形式,處理過程中將任務添加到隊列,然后在創建線程后自動啟動這些任務。線程池線程都是后臺線程。
2.線程池簡易架構
3.簡易線程池代碼(自行優化)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import java.util.List; /** * 線程接口 * * @Author yjian * @Date 14:49 2017/10/14 **/ public interface IThreadPool { //加入任務 void execute(Runnable task); //加入任務 void execute(Runnable[] tasks); //加入任務 void execute(List<Runnable> tasks); //銷毀線程 void destroy(); } |
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicLong; /** * 線程實現類(簡易實現,自行優化.提供思路) * * @Author yjian * @Date 14:49 2017/10/14 **/ @SuppressWarnings ( "ALL" ) public class ThreadPoolImpl implements IThreadPool { //默認開啟線程個數 static int WORKER_NUMBER = 5 ; //完成任務線程數 可見性 static volatile int sumCount = 0 ; //任務隊列 list非線程安全,可以優化為BlockingQueue static List<Runnable> taskQueue = new LinkedList<Runnable>(); //線程工作組 WorkerThread[] workThreads; //原子性 static AtomicLong threadNum = new AtomicLong(); static ThreadPoolImpl threadPool; //構造方法 public ThreadPoolImpl() { this (WORKER_NUMBER); } public ThreadPoolImpl( int workerNum) { this .WORKER_NUMBER = workerNum; //開辟工作線程空間 workThreads = new WorkerThread[WORKER_NUMBER]; //開始創建工作線程 for ( int i = 0 ; i < WORKER_NUMBER; i++) { workThreads[i] = new WorkerThread(); Thread thread = new Thread(workThreads[i], "ThreadPool-worker" + threadNum.incrementAndGet()); System.out.println( "初始化線程數" + (i + 1 ) + "---------當前線程名稱:" + thread.getName()); thread.start(); } } @Override public String toString() { return "工作線程數量為" + WORKER_NUMBER + "已完成的任務數" + sumCount + "等待任務數量" + taskQueue.size(); } //獲取線程池 public static IThreadPool getThreadPool() { return getThreadPool(WORKER_NUMBER); } public static IThreadPool getThreadPool( int workerNum) { //容錯性,如果小于等于0就默認線程數 if (workerNum <= 0 ) { workerNum = WORKER_NUMBER; } if (threadPool == null ) { threadPool = new ThreadPoolImpl(workerNum); } return threadPool; } @Override public void execute(Runnable task) { synchronized (taskQueue) { taskQueue.add(task); taskQueue.notifyAll(); } } @Override public void execute(Runnable[] tasks) { synchronized (taskQueue) { for (Runnable task : tasks) { taskQueue.add(task); } taskQueue.notifyAll(); } } @Override public void execute(List<Runnable> tasks) { synchronized (taskQueue) { for (Runnable task : tasks) { taskQueue.add(task); } taskQueue.notifyAll(); } } @Override public void destroy() { //循環是否還存在任務,如果存在等待20毫秒處理時間 while (!taskQueue.isEmpty()) { try { Thread.sleep( 20 ); } catch (InterruptedException e) { e.printStackTrace(); } } //如果任務隊列已處理完成,銷毀線程,清空任務 for ( int i = 0 ; i < WORKER_NUMBER; i++) { workThreads[i].setWorkerFlag(); workThreads[i] = null ; } threadPool = null ; taskQueue.clear(); } //創建工作線程池 class WorkerThread extends Thread { //用來標識當前線程屬于活動可用狀態 private boolean isRunning = true ; @Override public void run() { Runnable runnable = null ; //死循環 while (isRunning) { //非線程安全,所以采用同步鎖 synchronized (taskQueue) { while (isRunning && taskQueue.isEmpty()) { try { //如果任務隊列為空,等待20毫秒 監聽任務到達 taskQueue.wait( 20 ); } catch (Exception e) { e.printStackTrace(); } } //任務隊列不為空 if (!taskQueue.isEmpty()) { runnable = taskQueue.remove( 0 ); //獲取第一個任務 } } if (runnable != null ) { runnable.run(); } sumCount++; runnable = null ; } } //銷毀線程 public void setWorkerFlag() { isRunning = false ; } } } |
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
|
import java.util.ArrayList; import java.util.List; /** * 測試類 * * @Author yjian * @Date 15:37 2017/10/14 **/ public class ThreadPoolTest { public static void main(String[] args) { //獲取線程池 IThreadPool t = ThreadPoolImpl.getThreadPool( 20 ); List<Runnable> taskList = new ArrayList<Runnable>(); for ( int i = 0 ; i < 100 ; i++) { taskList.add( new Task()); } //執行任務 t.execute(taskList); System.out.println(t); //銷毀線程 t.destroy(); System.out.println(t); } static class Task implements Runnable { private static volatile int i = 1 ; @Override public void run() { System.out.println( "當前處理的線程:" + Thread.currentThread().getName() + " 執行任務" + (i++) + " 完成" ); } } } |
對spring源碼研究的,仔細查看代碼用了哪幾種spring常用的模式。寫程序的規范應該和spring一樣。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。