1. java5线程并发库新知识介绍
2.线程并发库案例分析
1 package com.itcast.family;
2
3 import java.util.concurrent.ExecutorService;
4 import java.util.concurrent.Executors;
5 import java.util.concurrent.TimeUnit;
6
7 public class ThreadPoolTest {
8
9 /**
10 * @param args
11 */
12 public static void main(String[] args) {
13
14 //1.new数量为3的固定的线程池;工具类一般都带着s;
15 //ExecutorService threadPool = Executors.newFixedThreadPool(3);
16 //2.缓存线程池,好处:需要几个线程就创建几个;不创建多余也不少创建
17 //ExecutorService threadPool = Executors.newCachedThreadPool();
18 //单例线程,就好比单独创建一个线程;好处:该线程死了立刻又创建出一个,可以解决线程死后重新启动的问题
19 ExecutorService threadPool = Executors.newSingleThreadExecutor();
20
21 for (int i = 1; i <= 10; i++) {
22 // 匿名内部类中不能必须使用最终变量;
23 final int task = i;
24 threadPool.execute(new Runnable() {
25 @Override
26 public void run() {
27 for (int j = 1; j <= 10; j++) {
28 try {
29 Thread.sleep(20);
30 } catch (InterruptedException e) {
31 e.printStackTrace();
32 }
33 System.out.println(Thread.currentThread().getName()+" is looping of "+j+" for task of "+task);
34 }
35 }
36 });
37 }
38 System.out.println("all of 10 takes have committed!!");
39 //线程池中没有任务了就结束线程池,如果不使用就会就算线程池中所有任务都结束了但是线程还在等着运行,不结束
40 //threadPool.shutdown();
41 /*使用shutdown()方法的巡行结果:
42 * 分析下面结果发现循环了10次即这个任务都调用的线程池中的这三个线程,每个任务又重复执行10次
43 all of 10 takes have committed!!
44 pool-1-thread-1 is looping of 1 for task of 1
45 pool-1-thread-3 is looping of 1 for task of 3
46 pool-1-thread-2 is looping of 1 for task of 2
47 pool-1-thread-3 is looping of 2 for task of 3
48 pool-1-thread-1 is looping of 2 for task of 1
49 pool-1-thread-2 is looping of 2 for task of 2
50 pool-1-thread-3 is looping of 3 for task of 3
51 pool-1-thread-1 is looping of 3 for task of 1
52 pool-1-thread-2 is looping of 3 for task of 2
53 pool-1-thread-3 is looping of 4 for task of 3
54 pool-1-thread-1 is looping of 4 for task of 1
55 pool-1-thread-2 is looping of 4 for task of 2
56 pool-1-thread-1 is looping of 5 for task of 1
57 pool-1-thread-3 is looping of 5 for task of 3
58 pool-1-thread-2 is looping of 5 for task of 2
59 pool-1-thread-1 is looping of 6 for task of 1
60 pool-1-thread-3 is looping of 6 for task of 3
61 pool-1-thread-2 is looping of 6 for task of 2
62 pool-1-thread-3 is looping of 7 for task of 3
63 pool-1-thread-1 is looping of 7 for task of 1
64 pool-1-thread-2 is looping of 7 for task of 2
65 pool-1-thread-1 is looping of 8 for task of 1
66 pool-1-thread-3 is looping of 8 for task of 3
67 pool-1-thread-2 is looping of 8 for task of 2
68 pool-1-thread-1 is looping of 9 for task of 1
69 pool-1-thread-3 is looping of 9 for task of 3
70 pool-1-thread-2 is looping of 9 for task of 2
71 pool-1-thread-1 is looping of 10 for task of 1
72 pool-1-thread-3 is looping of 10 for task of 3
73 pool-1-thread-2 is looping of 10 for task of 2
74 pool-1-thread-3 is looping of 1 for task of 5
75 pool-1-thread-1 is looping of 1 for task of 4
76 pool-1-thread-2 is looping of 1 for task of 6
77 pool-1-thread-3 is looping of 2 for task of 5
78 pool-1-thread-1 is looping of 2 for task of 4
79 pool-1-thread-2 is looping of 2 for task of 6
80 pool-1-thread-1 is looping of 3 for task of 4
81 pool-1-thread-3 is looping of 3 for task of 5
82 pool-1-thread-2 is looping of 3 for task of 6
83 pool-1-thread-1 is looping of 4 for task of 4
84 pool-1-thread-3 is looping of 4 for task of 5
85 pool-1-thread-2 is looping of 4 for task of 6
86 pool-1-thread-1 is looping of 5 for task of 4
87 pool-1-thread-3 is looping of 5 for task of 5
88 pool-1-thread-2 is looping of 5 for task of 6
89 pool-1-thread-1 is looping of 6 for task of 4
90 pool-1-thread-3 is looping of 6 for task of 5
91 pool-1-thread-2 is looping of 6 for task of 6
92 pool-1-thread-1 is looping of 7 for task of 4
93 pool-1-thread-3 is looping of 7 for task of 5
94 pool-1-thread-2 is looping of 7 for task of 6
95 pool-1-thread-3 is looping of 8 for task of 5
96 pool-1-thread-1 is looping of 8 for task of 4
97 pool-1-thread-2 is looping of 8 for task of 6
98 pool-1-thread-1 is looping of 9 for task of 4
99 pool-1-thread-3 is looping of 9 for task of 5
100 pool-1-thread-2 is looping of 9 for task of 6
101 pool-1-thread-3 is looping of 10 for task of 5
102 pool-1-thread-1 is looping of 10 for task of 4
103 pool-1-thread-2 is looping of 10 for task of 6
104 pool-1-thread-1 is looping of 1 for task of 8
105 pool-1-thread-3 is looping of 1 for task of 7
106 pool-1-thread-2 is looping of 1 for task of 9
107 pool-1-thread-3 is looping of 2 for task of 7
108 pool-1-thread-1 is looping of 2 for task of 8
109 pool-1-thread-2 is looping of 2 for task of 9
110 pool-1-thread-1 is looping of 3 for task of 8
111 pool-1-thread-3 is looping of 3 for task of 7
112 pool-1-thread-2 is looping of 3 for task of 9
113 pool-1-thread-1 is looping of 4 for task of 8
114 pool-1-thread-3 is looping of 4 for task of 7
115 pool-1-thread-2 is looping of 4 for task of 9
116 pool-1-thread-1 is looping of 5 for task of 8
117 pool-1-thread-3 is looping of 5 for task of 7
118 pool-1-thread-2 is looping of 5 for task of 9
119 pool-1-thread-1 is looping of 6 for task of 8
120 pool-1-thread-3 is looping of 6 for task of 7
121 pool-1-thread-2 is looping of 6 for task of 9
122 pool-1-thread-1 is looping of 7 for task of 8
123 pool-1-thread-3 is looping of 7 for task of 7
124 pool-1-thread-2 is looping of 7 for task of 9
125 pool-1-thread-3 is looping of 8 for task of 7
126 pool-1-thread-1 is looping of 8 for task of 8
127 pool-1-thread-2 is looping of 8 for task of 9
128 pool-1-thread-3 is looping of 9 for task of 7
129 pool-1-thread-1 is looping of 9 for task of 8
130 pool-1-thread-2 is looping of 9 for task of 9
131 pool-1-thread-1 is looping of 10 for task of 8
132 pool-1-thread-3 is looping of 10 for task of 7
133 pool-1-thread-2 is looping of 10 for task of 9
134 pool-1-thread-1 is looping of 1 for task of 10
135 pool-1-thread-1 is looping of 2 for task of 10
136 pool-1-thread-1 is looping of 3 for task of 10
137 pool-1-thread-1 is looping of 4 for task of 10
138 pool-1-thread-1 is looping of 5 for task of 10
139 pool-1-thread-1 is looping of 6 for task of 10
140 pool-1-thread-1 is looping of 7 for task of 10
141 pool-1-thread-1 is looping of 8 for task of 10
142 pool-1-thread-1 is looping of 9 for task of 10
143 pool-1-thread-1 is looping of 10 for task of 10*/
144
145
146
147 //结束当前线程
148 // threadPool.shutdownNow();
149 /*
150 * 运行结果:
151 * 主要看下面的结果,暂且忽略错误提示;上面的是十个线程,这个却是三个,
152 * 这是因为结束当前线程,当前就三个线程。所以直接结束了不管后面没有线程的
153 * 其他七个任务。
154 * all of 10 takes have committed!!
155 java.lang.InterruptedException: sleep interrupted
156 at java.lang.Thread.sleep(Native Method)
157 at com.itcast.family.ThreadPoolTest$1.run(ThreadPoolTest.java:25)
158 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
159 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
160 at java.lang.Thread.run(Thread.java:619)
161 pool-1-thread-1 is looping of 1 for task of 1java.lang.InterruptedException: sleep interrupted
162
163 at java.lang.Thread.sleep(Native Method)
164 at com.itcast.family.ThreadPoolTest$1.run(ThreadPoolTest.java:25)
165 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
166 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
167 at java.lang.Thread.run(Thread.java:619)
168 pool-1-thread-2 is looping of 1 for task of 2
169 java.lang.InterruptedException: sleep interrupted
170 at java.lang.Thread.sleep(Native Method)
171 at com.itcast.family.ThreadPoolTest$1.run(ThreadPoolTest.java:25)
172 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
173 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
174 at java.lang.Thread.run(Thread.java:619)
175 pool-1-thread-3 is looping of 1 for task of 3
176 pool-1-thread-1 is looping of 2 for task of 1
177 pool-1-thread-2 is looping of 2 for task of 2
178 pool-1-thread-3 is looping of 2 for task of 3
179 pool-1-thread-2 is looping of 3 for task of 2
180 pool-1-thread-1 is looping of 3 for task of 1
181 pool-1-thread-3 is looping of 3 for task of 3
182 pool-1-thread-1 is looping of 4 for task of 1
183 pool-1-thread-2 is looping of 4 for task of 2
184 pool-1-thread-3 is looping of 4 for task of 3
185 pool-1-thread-2 is looping of 5 for task of 2
186 pool-1-thread-1 is looping of 5 for task of 1
187 pool-1-thread-3 is looping of 5 for task of 3
188 pool-1-thread-1 is looping of 6 for task of 1
189 pool-1-thread-2 is looping of 6 for task of 2
190 pool-1-thread-3 is looping of 6 for task of 3
191 pool-1-thread-2 is looping of 7 for task of 2
192 pool-1-thread-1 is looping of 7 for task of 1
193 pool-1-thread-3 is looping of 7 for task of 3
194 pool-1-thread-2 is looping of 8 for task of 2
195 pool-1-thread-1 is looping of 8 for task of 1
196 pool-1-thread-3 is looping of 8 for task of 3
197 pool-1-thread-1 is looping of 9 for task of 1
198 pool-1-thread-2 is looping of 9 for task of 2
199 pool-1-thread-3 is looping of 9 for task of 3
200 pool-1-thread-2 is looping of 10 for task of 2
201 pool-1-thread-1 is looping of 10 for task of 1
202 pool-1-thread-3 is looping of 10 for task of 3
203
204 */
205
206
207
208 /*动态变化即缓存那个的运行结果:
209 * 需要几个线程就new出几个线程;这里是个是个任务就new出了十个线程
210 * 而不是像第一种那样是个任务三个线程
211 * all of 10 takes have committed!!
212 pool-1-thread-2 is looping of 1 for task of 2
213 pool-1-thread-4 is looping of 1 for task of 4
214 pool-1-thread-6 is looping of 1 for task of 6
215 pool-1-thread-8 is looping of 1 for task of 8
216 pool-1-thread-10 is looping of 1 for task of 10
217 pool-1-thread-1 is looping of 1 for task of 1
218 pool-1-thread-3 is looping of 1 for task of 3
219 pool-1-thread-5 is looping of 1 for task of 5
220 pool-1-thread-7 is looping of 1 for task of 7
221 pool-1-thread-9 is looping of 1 for task of 9
222 pool-1-thread-6 is looping of 2 for task of 6
223 pool-1-thread-4 is looping of 2 for task of 4
224 pool-1-thread-2 is looping of 2 for task of 2
225 pool-1-thread-1 is looping of 2 for task of 1
226 pool-1-thread-8 is looping of 2 for task of 8
227 pool-1-thread-10 is looping of 2 for task of 10
228 pool-1-thread-3 is looping of 2 for task of 3
229 pool-1-thread-5 is looping of 2 for task of 5
230 pool-1-thread-7 is looping of 2 for task of 7
231 pool-1-thread-9 is looping of 2 for task of 9
232 pool-1-thread-6 is looping of 3 for task of 6
233 pool-1-thread-4 is looping of 3 for task of 4
234 pool-1-thread-1 is looping of 3 for task of 1
235 pool-1-thread-2 is looping of 3 for task of 2
236 pool-1-thread-8 is looping of 3 for task of 8
237 pool-1-thread-5 is looping of 3 for task of 5
238 pool-1-thread-3 is looping of 3 for task of 3
239 pool-1-thread-10 is looping of 3 for task of 10
240 pool-1-thread-9 is looping of 3 for task of 9
241 pool-1-thread-7 is looping of 3 for task of 7
242 pool-1-thread-6 is looping of 4 for task of 6
243 pool-1-thread-1 is looping of 4 for task of 1
244 pool-1-thread-4 is looping of 4 for task of 4
245 pool-1-thread-8 is looping of 4 for task of 8
246 pool-1-thread-2 is looping of 4 for task of 2
247 pool-1-thread-3 is looping of 4 for task of 3
248 pool-1-thread-5 is looping of 4 for task of 5
249 pool-1-thread-10 is looping of 4 for task of 10
250 pool-1-thread-7 is looping of 4 for task of 7
251 pool-1-thread-9 is looping of 4 for task of 9
252 pool-1-thread-1 is looping of 5 for task of 1
253 pool-1-thread-6 is looping of 5 for task of 6
254 pool-1-thread-4 is looping of 5 for task of 4
255 pool-1-thread-2 is looping of 5 for task of 2
256 pool-1-thread-5 is looping of 5 for task of 5
257 pool-1-thread-3 is looping of 5 for task of 3
258 pool-1-thread-8 is looping of 5 for task of 8
259 pool-1-thread-10 is looping of 5 for task of 10
260 pool-1-thread-7 is looping of 5 for task of 7
261 pool-1-thread-9 is looping of 5 for task of 9
262 pool-1-thread-1 is looping of 6 for task of 1
263 pool-1-thread-6 is looping of 6 for task of 6
264 pool-1-thread-4 is looping of 6 for task of 4
265 pool-1-thread-5 is looping of 6 for task of 5
266 pool-1-thread-3 is looping of 6 for task of 3
267 pool-1-thread-2 is looping of 6 for task of 2
268 pool-1-thread-10 is looping of 6 for task of 10
269 pool-1-thread-8 is looping of 6 for task of 8
270 pool-1-thread-7 is looping of 6 for task of 7
271 pool-1-thread-9 is looping of 6 for task of 9
272 pool-1-thread-1 is looping of 7 for task of 1
273 pool-1-thread-4 is looping of 7 for task of 4
274 pool-1-thread-6 is looping of 7 for task of 6
275 pool-1-thread-2 is looping of 7 for task of 2
276 pool-1-thread-3 is looping of 7 for task of 3
277 pool-1-thread-5 is looping of 7 for task of 5
278 pool-1-thread-8 is looping of 7 for task of 8
279 pool-1-thread-10 is looping of 7 for task of 10
280 pool-1-thread-7 is looping of 7 for task of 7
281 pool-1-thread-9 is looping of 7 for task of 9
282 pool-1-thread-1 is looping of 8 for task of 1
283 pool-1-thread-6 is looping of 8 for task of 6
284 pool-1-thread-4 is looping of 8 for task of 4
285 pool-1-thread-2 is looping of 8 for task of 2
286 pool-1-thread-5 is looping of 8 for task of 5
287 pool-1-thread-3 is looping of 8 for task of 3
288 pool-1-thread-8 is looping of 8 for task of 8
289 pool-1-thread-10 is looping of 8 for task of 10
290 pool-1-thread-7 is looping of 8 for task of 7
291 pool-1-thread-9 is looping of 8 for task of 9
292 pool-1-thread-1 is looping of 9 for task of 1
293 pool-1-thread-6 is looping of 9 for task of 6
294 pool-1-thread-4 is looping of 9 for task of 4
295 pool-1-thread-5 is looping of 9 for task of 5
296 pool-1-thread-3 is looping of 9 for task of 3
297 pool-1-thread-2 is looping of 9 for task of 2
298 pool-1-thread-8 is looping of 9 for task of 8
299 pool-1-thread-10 is looping of 9 for task of 10
300 pool-1-thread-7 is looping of 9 for task of 7
301 pool-1-thread-9 is looping of 9 for task of 9
302 pool-1-thread-1 is looping of 10 for task of 1
303 pool-1-thread-3 is looping of 10 for task of 3
304 pool-1-thread-5 is looping of 10 for task of 5
305 pool-1-thread-2 is looping of 10 for task of 2
306 pool-1-thread-4 is looping of 10 for task of 4
307 pool-1-thread-6 is looping of 10 for task of 6
308 pool-1-thread-8 is looping of 10 for task of 8
309 pool-1-thread-7 is looping of 10 for task of 7
310 pool-1-thread-9 is looping of 10 for task of 9
311 pool-1-thread-10 is looping of 10 for task of 10
312
313 */
314
315
316 //java新技术中的定时器的使用;相当于一次性炸弹
317 Executors.newScheduledThreadPool(3).schedule(new Runnable() {
318
319 @Override
320 public void run() {
321 System.out.println("bombing!!!");
322 }
323 },4,
324 TimeUnit.SECONDS);
325
326 //连环炸弹
327 Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {
328
329 @Override
330 public void run() {
331 System.out.println("bombing!!!");
332 }
333 },
334 4, //隔多少秒炸一下
335 2, //隔多少秒再炸
336 TimeUnit.SECONDS);
337 }
338 }