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. // threadPool.shutdownNow();
  147. /*
  148. * 运行结果:
  149. * 主要看下面的结果,暂且忽略错误提示;上面的是十个线程,这个却是三个,
  150. * 这是因为结束当前线程,当前就三个线程。所以直接结束了不管后面没有线程的
  151. * 其他七个任务。
  152. * all of 10 takes have committed!!
  153. java.lang.InterruptedException: sleep interrupted
  154. at java.lang.Thread.sleep(Native Method)
  155. at com.itcast.family.ThreadPoolTest$1.run(ThreadPoolTest.java:25)
  156. at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  157. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  158. at java.lang.Thread.run(Thread.java:619)
  159. pool-1-thread-1 is looping of 1 for task of 1java.lang.InterruptedException: sleep interrupted
  160.  
  161. at java.lang.Thread.sleep(Native Method)
  162. at com.itcast.family.ThreadPoolTest$1.run(ThreadPoolTest.java:25)
  163. at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  164. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  165. at java.lang.Thread.run(Thread.java:619)
  166. pool-1-thread-2 is looping of 1 for task of 2
  167. java.lang.InterruptedException: sleep interrupted
  168. at java.lang.Thread.sleep(Native Method)
  169. at com.itcast.family.ThreadPoolTest$1.run(ThreadPoolTest.java:25)
  170. at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  171. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  172. at java.lang.Thread.run(Thread.java:619)
  173. pool-1-thread-3 is looping of 1 for task of 3
  174. pool-1-thread-1 is looping of 2 for task of 1
  175. pool-1-thread-2 is looping of 2 for task of 2
  176. pool-1-thread-3 is looping of 2 for task of 3
  177. pool-1-thread-2 is looping of 3 for task of 2
  178. pool-1-thread-1 is looping of 3 for task of 1
  179. pool-1-thread-3 is looping of 3 for task of 3
  180. pool-1-thread-1 is looping of 4 for task of 1
  181. pool-1-thread-2 is looping of 4 for task of 2
  182. pool-1-thread-3 is looping of 4 for task of 3
  183. pool-1-thread-2 is looping of 5 for task of 2
  184. pool-1-thread-1 is looping of 5 for task of 1
  185. pool-1-thread-3 is looping of 5 for task of 3
  186. pool-1-thread-1 is looping of 6 for task of 1
  187. pool-1-thread-2 is looping of 6 for task of 2
  188. pool-1-thread-3 is looping of 6 for task of 3
  189. pool-1-thread-2 is looping of 7 for task of 2
  190. pool-1-thread-1 is looping of 7 for task of 1
  191. pool-1-thread-3 is looping of 7 for task of 3
  192. pool-1-thread-2 is looping of 8 for task of 2
  193. pool-1-thread-1 is looping of 8 for task of 1
  194. pool-1-thread-3 is looping of 8 for task of 3
  195. pool-1-thread-1 is looping of 9 for task of 1
  196. pool-1-thread-2 is looping of 9 for task of 2
  197. pool-1-thread-3 is looping of 9 for task of 3
  198. pool-1-thread-2 is looping of 10 for task of 2
  199. pool-1-thread-1 is looping of 10 for task of 1
  200. pool-1-thread-3 is looping of 10 for task of 3
  201.  
  202. */
  203.  
  204. /*动态变化即缓存那个的运行结果:
  205. * 需要几个线程就new出几个线程;这里是个是个任务就new出了十个线程
  206. * 而不是像第一种那样是个任务三个线程
  207. * all of 10 takes have committed!!
  208. pool-1-thread-2 is looping of 1 for task of 2
  209. pool-1-thread-4 is looping of 1 for task of 4
  210. pool-1-thread-6 is looping of 1 for task of 6
  211. pool-1-thread-8 is looping of 1 for task of 8
  212. pool-1-thread-10 is looping of 1 for task of 10
  213. pool-1-thread-1 is looping of 1 for task of 1
  214. pool-1-thread-3 is looping of 1 for task of 3
  215. pool-1-thread-5 is looping of 1 for task of 5
  216. pool-1-thread-7 is looping of 1 for task of 7
  217. pool-1-thread-9 is looping of 1 for task of 9
  218. pool-1-thread-6 is looping of 2 for task of 6
  219. pool-1-thread-4 is looping of 2 for task of 4
  220. pool-1-thread-2 is looping of 2 for task of 2
  221. pool-1-thread-1 is looping of 2 for task of 1
  222. pool-1-thread-8 is looping of 2 for task of 8
  223. pool-1-thread-10 is looping of 2 for task of 10
  224. pool-1-thread-3 is looping of 2 for task of 3
  225. pool-1-thread-5 is looping of 2 for task of 5
  226. pool-1-thread-7 is looping of 2 for task of 7
  227. pool-1-thread-9 is looping of 2 for task of 9
  228. pool-1-thread-6 is looping of 3 for task of 6
  229. pool-1-thread-4 is looping of 3 for task of 4
  230. pool-1-thread-1 is looping of 3 for task of 1
  231. pool-1-thread-2 is looping of 3 for task of 2
  232. pool-1-thread-8 is looping of 3 for task of 8
  233. pool-1-thread-5 is looping of 3 for task of 5
  234. pool-1-thread-3 is looping of 3 for task of 3
  235. pool-1-thread-10 is looping of 3 for task of 10
  236. pool-1-thread-9 is looping of 3 for task of 9
  237. pool-1-thread-7 is looping of 3 for task of 7
  238. pool-1-thread-6 is looping of 4 for task of 6
  239. pool-1-thread-1 is looping of 4 for task of 1
  240. pool-1-thread-4 is looping of 4 for task of 4
  241. pool-1-thread-8 is looping of 4 for task of 8
  242. pool-1-thread-2 is looping of 4 for task of 2
  243. pool-1-thread-3 is looping of 4 for task of 3
  244. pool-1-thread-5 is looping of 4 for task of 5
  245. pool-1-thread-10 is looping of 4 for task of 10
  246. pool-1-thread-7 is looping of 4 for task of 7
  247. pool-1-thread-9 is looping of 4 for task of 9
  248. pool-1-thread-1 is looping of 5 for task of 1
  249. pool-1-thread-6 is looping of 5 for task of 6
  250. pool-1-thread-4 is looping of 5 for task of 4
  251. pool-1-thread-2 is looping of 5 for task of 2
  252. pool-1-thread-5 is looping of 5 for task of 5
  253. pool-1-thread-3 is looping of 5 for task of 3
  254. pool-1-thread-8 is looping of 5 for task of 8
  255. pool-1-thread-10 is looping of 5 for task of 10
  256. pool-1-thread-7 is looping of 5 for task of 7
  257. pool-1-thread-9 is looping of 5 for task of 9
  258. pool-1-thread-1 is looping of 6 for task of 1
  259. pool-1-thread-6 is looping of 6 for task of 6
  260. pool-1-thread-4 is looping of 6 for task of 4
  261. pool-1-thread-5 is looping of 6 for task of 5
  262. pool-1-thread-3 is looping of 6 for task of 3
  263. pool-1-thread-2 is looping of 6 for task of 2
  264. pool-1-thread-10 is looping of 6 for task of 10
  265. pool-1-thread-8 is looping of 6 for task of 8
  266. pool-1-thread-7 is looping of 6 for task of 7
  267. pool-1-thread-9 is looping of 6 for task of 9
  268. pool-1-thread-1 is looping of 7 for task of 1
  269. pool-1-thread-4 is looping of 7 for task of 4
  270. pool-1-thread-6 is looping of 7 for task of 6
  271. pool-1-thread-2 is looping of 7 for task of 2
  272. pool-1-thread-3 is looping of 7 for task of 3
  273. pool-1-thread-5 is looping of 7 for task of 5
  274. pool-1-thread-8 is looping of 7 for task of 8
  275. pool-1-thread-10 is looping of 7 for task of 10
  276. pool-1-thread-7 is looping of 7 for task of 7
  277. pool-1-thread-9 is looping of 7 for task of 9
  278. pool-1-thread-1 is looping of 8 for task of 1
  279. pool-1-thread-6 is looping of 8 for task of 6
  280. pool-1-thread-4 is looping of 8 for task of 4
  281. pool-1-thread-2 is looping of 8 for task of 2
  282. pool-1-thread-5 is looping of 8 for task of 5
  283. pool-1-thread-3 is looping of 8 for task of 3
  284. pool-1-thread-8 is looping of 8 for task of 8
  285. pool-1-thread-10 is looping of 8 for task of 10
  286. pool-1-thread-7 is looping of 8 for task of 7
  287. pool-1-thread-9 is looping of 8 for task of 9
  288. pool-1-thread-1 is looping of 9 for task of 1
  289. pool-1-thread-6 is looping of 9 for task of 6
  290. pool-1-thread-4 is looping of 9 for task of 4
  291. pool-1-thread-5 is looping of 9 for task of 5
  292. pool-1-thread-3 is looping of 9 for task of 3
  293. pool-1-thread-2 is looping of 9 for task of 2
  294. pool-1-thread-8 is looping of 9 for task of 8
  295. pool-1-thread-10 is looping of 9 for task of 10
  296. pool-1-thread-7 is looping of 9 for task of 7
  297. pool-1-thread-9 is looping of 9 for task of 9
  298. pool-1-thread-1 is looping of 10 for task of 1
  299. pool-1-thread-3 is looping of 10 for task of 3
  300. pool-1-thread-5 is looping of 10 for task of 5
  301. pool-1-thread-2 is looping of 10 for task of 2
  302. pool-1-thread-4 is looping of 10 for task of 4
  303. pool-1-thread-6 is looping of 10 for task of 6
  304. pool-1-thread-8 is looping of 10 for task of 8
  305. pool-1-thread-7 is looping of 10 for task of 7
  306. pool-1-thread-9 is looping of 10 for task of 9
  307. pool-1-thread-10 is looping of 10 for task of 10
  308.  
  309. */
  310.  
  311. //java新技术中的定时器的使用;相当于一次性炸弹
  312. Executors.newScheduledThreadPool(3).schedule(new Runnable() {
  313.  
  314. @Override
  315. public void run() {
  316. System.out.println("bombing!!!");
  317. }
  318. },4,
  319. TimeUnit.SECONDS);
  320.  
  321. //连环炸弹
  322. Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {
  323.  
  324. @Override
  325. public void run() {
  326. System.out.println("bombing!!!");
  327. }
  328. },
  329. 4, //隔多少秒炸一下
  330. 2, //隔多少秒再炸
  331. TimeUnit.SECONDS);
  332. }
  333. }

线程高级应用-心得4-java5线程并发库介绍,及新技术案例分析的更多相关文章

  1. 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析

    1.  HashSet与HashMap的联系与区别? 区别:前者是单列后者是双列,就是hashmap有键有值,hashset只有键: 联系:HashSet的底层就是HashMap,可以参考HashSe ...

  2. 线程高级应用-心得5-java5线程并发库中Lock和Condition实现线程同步通讯

    1.Lock相关知识介绍 好比我同时种了几块地的麦子,然后就等待收割.收割时,则是哪块先熟了,先收割哪块. 下面举一个面试题的例子来引出Lock缓存读写锁的案例,一个load()和get()方法返回值 ...

  3. 线程高级应用-心得7-java5线程并发库中阻塞队列Condition的应用及案例分析

    1.阻塞队列知识点 阻塞队列重要的有以下几个方法,具体用法可以参考帮助文档:区别说的很清楚,第一个种方法不阻塞直接抛异常:第二种方法是boolean型的,阻塞返回flase:第三种方法直接阻塞. 2. ...

  4. 线程高级应用-心得6-java5线程并发库中同步工具类(synchronizers),新知识大用途

    1.新知识普及 2. Semaphore工具类的使用案例 package com.java5.thread.newSkill; import java.util.concurrent.Executor ...

  5. Java复习——多线程与并发库

    开启一个线程 实现一个线程的方式有两种:继承Thread类.实现Runnable接口(也存在说三种的情况,第三种是使用线程并发库中的线程池创建一个线程).这两种方法都需要重写Run方法,具体的线程逻辑 ...

  6. Java多线程与并发库高级应用-java5线程并发库

    java5 中的线程并发库 主要在java.util.concurrent包中 还有 java.util.concurrent.atomic子包和java.util.concurrent.lock子包 ...

  7. java--加强之 Java5的线程并发库

    转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9945499 01. 传统线程技术回顾 创建线程的两种传统方式: 1.在Thread子类覆盖的r ...

  8. java架构《并发线程高级篇一》

    本章主要记录讲解并发线程的线程池.java.util.concurrent工具包里面的工具类. 一:Executor框架: Executors创建线程池的方法: newFixedThreadPool( ...

  9. Android多线程研究(7)——Java5中的线程并发库

    从这一篇开始我们将看看Java 5之后给我们添加的新的对线程操作的API,首先看看api文档: java.util.concurrent包含许多线程安全.测试良好.高性能的并发构建块,我们先看看ato ...

随机推荐

  1. 20160113006 asp.net实现ftp上传代码(解决大文件上传问题)

    using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using S ...

  2. poj3342 Party at Hali-Bula

    树形dp题,状态转移方程应该很好推,但一定要细心. http://poj.org/problem?id=3342 #include <cstdio> #include <cstrin ...

  3. ural 1113,jeep problem

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1113 网上的解答铺天盖地.我硬是花了两天才懂了点. wiki上的解释:https://e ...

  4. no branch 问题

    现象如下: lynn.feng:~/project/Git/M_MT6737_MP$ git branch -a* (no branch) a36_panasonic_l004 b36_panason ...

  5. uml中定义的关系详细详解

    uml定义的关系主要有六种:依赖.类属.关联.实现.聚合和组合.下面对其定义和表示方法逐一说明. 依赖(Dependency):元素A的变化会影响元素B,但反之不成立,那么B和A的关系是依赖关系,B依 ...

  6. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A

    Description You are given names of two days of the week. Please, determine whether it is possible th ...

  7. js实现元素边框闪烁功能

    <body> <input type="text" value="test" onclick="flash(this)"& ...

  8. MSComm函数说明(来自网络)

    CommPort 设置并返回端口号 void CMSComm::SetCommPort(short nNewValue) short CMSComm::GetCommPort() RThreshold ...

  9. HDU 5615 Jam's math problem

    Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is t ...

  10. SqlSever基础 datepart 获取一个日期的年份

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...