免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2726 | 回复: 1
打印 上一主题 下一主题

线程池的使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-06 17:14 |只看该作者 |倒序浏览
线程池的使用




1、通过创建一个拥有固定数目的线程的线程池
2、每次启动指定数目线程去执行任务,把线程的创建、维护交给线程池来管理
3、将多线程任务设置到定时任务中,观察线程执行情况

==============
如果不采用线程池,在我机器上运行大概35秒钟,程序就挂了!!!
  1. package threads;

  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Timer;
  5. import java.util.TimerTask;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Executors;

  8. /**
  9. * 线程池测试
  10. *
  11. * @author liuwei
  12. */
  13. public class TestThreadPool extends TimerTask
  14. {

  15. private List<TestBean> initData = new ArrayList<TestBean>();

  16. private ExecutorService services = null;

  17. private final int threadNum = 1000;

  18. private final int cycleNum = 80;

  19. // 初始化数据及线程池
  20. public TestThreadPool()
  21. {
  22. int n = threadNum * 2 + 3;
  23. int c = 1;
  24. for (int i = 1; i <= n; i++)
  25. {
  26. initData.add(new TestBean(cycleNum, String.valueOf(c)));
  27. c++;
  28. }

  29. // 查看系统cpu个数
  30. // int cpuNum = Runtime.getRuntime().availableProcessors();
  31. // System.out.println("cpu number : " + cpuNum);
  32. services = Executors.newFixedThreadPool(threadNum);
  33. }

  34. /**
  35. * 需要多线程执行的方法
  36. *
  37. * @param obj
  38. */
  39. public void toPrint(TestBean obj)
  40. {
  41. if (null == obj)
  42. {
  43. return;
  44. }

  45. int n = obj.getN();
  46. for (int i = 0; i < n; i++)
  47. {
  48. try
  49. {
  50. Thread.sleep(200);
  51. } catch (InterruptedException e)
  52. {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. }
  56. System.out.println("============" + Thread.currentThread().getName() + ", str : " + obj.getStr());
  57. }
  58. } // toPrint

  59. // 执行线程方法的线程类
  60. private class TestRun implements Runnable
  61. {
  62. private TestBean obj;

  63. public TestRun(TestBean obj)
  64. {
  65. this.obj = obj;
  66. }

  67. public void run()
  68. {
  69. toPrint(obj);
  70. }
  71. } // TestRun


  72. public void run()
  73. {
  74. int size = initData.size();
  75. int count = (size + threadNum - 1) / threadNum;
  76. System.out.println("count: " + count);
  77. for (int i = 0; i < count; i++)
  78. {
  79. System.out.println("====================" + i);
  80. for (int j = 0; j < threadNum; j++)
  81. {
  82. int index = i * threadNum + j;
  83. // System.out.println("index: "+index);
  84. if (index >= size)
  85. {
  86. break;
  87. }
  88. final TestBean obj = initData.get(i * threadNum + j);
  89. // System.out.println(obj.getN());
  90. // new Thread(new TestRun(obj)).start();
  91. services.execute(new TestRun(obj));

  92. }

  93. } // end-for-initData

  94. } // run


  95. /**
  96. * @param args
  97. */
  98. public static void main(String[] args)
  99. {
  100. // TODO Auto-generated method stub
  101. Timer timer = new Timer();
  102. TestThreadPool tt = new TestThreadPool();
  103. // 启动后立即执行,每隔15秒钟在执行一次
  104. timer.schedule(tt, 0, 15 * 1000);

  105. // 关闭线程池
  106. // tt.services.shutdown();
  107. }

  108. } // TestThreadPool


  109. class TestBean
  110. {
  111. private int n;
  112. private String str;

  113. public TestBean(int n, String str)
  114. {
  115. this.n = n;
  116. this.str = str;
  117. }

  118. public int getN()
  119. {
  120. return n;
  121. }

  122. public void setN(int n)
  123. {
  124. this.n = n;
  125. }

  126. public String getStr()
  127. {
  128. return str;
  129. }

  130. public void setStr(String str)
  131. {
  132. this.str = str;
  133. }
  134. } // TestBean
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-11-07 22:34 |只看该作者
晓得了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP