博弈Java讲义 - Java线程之ThreadGroup_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 博弈Java讲义 - Java线程之ThreadGroup

博弈Java讲义 - Java线程之ThreadGroup

 2014/11/27 22:09:44  boyitech  程序员俱乐部  我要评论(0)
  • 摘要:Java并发包里面提供了ThreadGroup类可以帮助我们有效地管理线程组。让我们来看一个生动的例子.Java线程组可以有线程对象或者子线程组组成。也就是说ThreadGroup可以是产生线程树。让我们看一个简单的例子,这个例子模仿一个搜索任务,将启动10个线程遍历获取指定目录下面的excel文件列表,一旦有一个线程完成遍历,将中断其余的9个任务。Java代码importjava.io.File;importjava.util.concurrent.TimeUnit
  • 标签:thread Java 博弈 线程

Java并发包里面提供了ThreadGroup类可以帮助我们有效地管理线程组。让我们来看一个生动的例子. Java线程组可以有线程对象或者子线程组组成。也就是说ThreadGroup可以是产生线程树。class="Apple-converted-space">?
? 让我们看一个简单的例子,这个例子模仿一个搜索任务,将启动10个线程遍历获取指定目录下面的excel文件列表,一旦有一个线程完成遍历,将中断其余的9个任务。?


Java代码?
  1. import?java.io.File;??
  2. import?java.util.concurrent.TimeUnit;??
  3. ??
  4. public?class?SearchTask?implements?Runnable?{??
  5. ??
  6. ????private?String?rootDir;??
  7. ????private?SearchResult?result;??
  8. ??
  9. ????public?SearchTask(String?rootDir,?SearchResult?result)?{??
  10. ????????super();??
  11. ????????this.rootDir?=?rootDir;??
  12. ????????this.result?=?result;??
  13. ????}??
  14. ??
  15. ????public?String?getRootDir()?{??
  16. ????????return?rootDir;??
  17. ????}??
  18. ??
  19. ????public?void?setRootDir(String?rootDir)?{??
  20. ????????this.rootDir?=?rootDir;??
  21. ????}??
  22. ??????
  23. ????public?SearchResult?getResult()?{??
  24. ????????return?result;??
  25. ????}??
  26. ??????
  27. ????public?void?setResult(SearchResult?result)?{??
  28. ????????this.result?=?result;??
  29. ????}??
  30. ??
  31. ????@Override??
  32. ????public?void?run()?{??
  33. ????????String?name?=?Thread.currentThread().getName();??
  34. ????????System.out.printf("Thread?%s:?Start\n",name);??
  35. ????????try{??
  36. ????????????doSearch(new?File(this.rootDir));??
  37. ????????????result.setTaskName(name);??
  38. ????????}catch(InterruptedException?e){??
  39. ????????????System.out.printf("Thread?%s:?Interrupted\n",name);??
  40. ????????????return;??
  41. ????????}??
  42. ????????System.out.printf("Thread?%s:?End\n",name);??
  43. ????}??
  44. ??????
  45. ????private?void?doSearch(File?root)?throws?InterruptedException{?????
  46. ????????if(root?!=?null?&&?root.isDirectory()){??
  47. ????????????TimeUnit.SECONDS.sleep(1);??
  48. ????????????File[]?files?=?root.listFiles();??
  49. ????????????if(?files?!=?null?){??
  50. ????????????????for(File?file?:?files){??
  51. ????????????????????if(file.isFile()?&&?file.getName().endsWith(".xls")){??
  52. ????????????????????????result.getFiles().add(file.getAbsolutePath());??
  53. ????????????????????????result.increaseCount();??
  54. ????????????????????}else?if(file.isDirectory()){??
  55. ????????????????????????doSearch(file);??
  56. ????????????????????}??
  57. ????????????????}??
  58. ????????????}??
  59. ????????}??
  60. ????}??
  61. }??
  62. package?com.concurrent.exercise.threadgroup;??
  63. ??
  64. import?java.util.ArrayList;??
  65. import?java.util.List;??
  66. ??
  67. public?class?SearchResult?{??
  68. ??????
  69. ????private?int?count?=?0;??
  70. ????private?String?taskName;??
  71. ????private?List<String>?files?=?new?ArrayList<String>();??
  72. ??????
  73. ????public?String?getTaskName()?{??
  74. ????????return?taskName;??
  75. ????}??
  76. ??
  77. ????public?void?setTaskName(String?taskName)?{??
  78. ????????this.taskName?=?taskName;??
  79. ????}??
  80. ??
  81. ????public?void?setCount(int?count)?{??
  82. ????????this.count?=?count;??
  83. ????}??
  84. ??
  85. ????public?void?setFiles(List<String>?files)?{??
  86. ????????this.files?=?files;??
  87. ????}??
  88. ??
  89. ????public?int?getCount()?{??
  90. ????????return?count;??
  91. ????}??
  92. ??
  93. ????public?void?increaseCount()?{??
  94. ????????count++;??
  95. ????}??
  96. ??
  97. ????public?List<String>?getFiles(){??
  98. ????????return?files;??
  99. ????}??
  100. ??????
  101. }??
  102. ??
  103. import?java.util.concurrent.TimeUnit;??
  104. ??
  105. public?class?TestThreadGroup?{??
  106. ??
  107. ????public?static?void?main(String[]?args){??
  108. ????????String?searchDir?=?"D:\\";??
  109. ????????ThreadGroup?threadGroup?=?new?ThreadGroup("Searcher");??
  110. ????????for(int?i?=?0;?i?<?3;?i++){??
  111. ????????????SearchTask?task?=?new?SearchTask(searchDir,new?SearchResult());??
  112. ????????????Thread?thread?=?new?Thread(threadGroup,?task);??
  113. ????????????thread.start();??
  114. ????????????try{??
  115. ????????????????TimeUnit.SECONDS.sleep(2);??
  116. ????????????}catch(InterruptedException?e)?{??
  117. ????????????????e.printStackTrace();??
  118. ????????????}??
  119. ????????}??
  120. ????????waitFinish(threadGroup);??
  121. ????????threadGroup.interrupt();??
  122. ????}??
  123. ??????
  124. ????private?static?void?waitFinish(ThreadGroup?threadGroup)?{??
  125. ????????while?(threadGroup.activeCount()?>?2)?{??
  126. ????????????try?{??
  127. ????????????????list(threadGroup);??
  128. ????????????????TimeUnit.SECONDS.sleep(1);??
  129. ????????????}?catch?(InterruptedException?e)?{??
  130. ????????????????e.printStackTrace();??
  131. ????????????}??
  132. ????????}??
  133. ????}??
  134. ??
  135. ????private?static?void?list(ThreadGroup?threadGroup)?{??
  136. ????????assert(threadGroup?!=?null);??
  137. ????????threadGroup.list();??
  138. ????}??
  139. ??????
  140. }??


? 在启动三个线程后,进入waitFinish方法,当其中一个线程结束后,waitFinish立即对出并调用ThreadGroup终止剩下的线程。?
? 由于ThreadGroup存储了线程和子线程组对象,可以使用ThreadGroup对一组线程执行统一的操作,比如interrupt.?
? 可以参考Java API Doc知道更多关于ThreadGroup的方法。?

上一篇: 博弈Java讲义 - Java线程之uncontrolled exceptions 下一篇: 没有下一篇了!
发表评论
用户名: 匿名