1.  java5线程并发库新知识介绍

2.线程并发库案例分析

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

 

版权声明:本文为cxxjohnson原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/cxxjohnson/p/6261890.html