首页 > 编程语言 > 详细

java 5 线程池

时间:2014-10-26 21:11:17      阅读:258      评论:0      收藏:0      [点我收藏+]
 1 import java.util.concurrent.ExecutorService;
 2 import java.util.concurrent.Executors;
 3 
 4 public class ThreadPoolTest
 5 {
 6     public static void main(String[] args)
 7     {
 8         ExecutorService threadPool = Executors.newFixedThreadPool(3);
 9         
10         for(int i = 0; i < 10; i++)
11         {
12             final String taskName = "task " + i;
13             threadPool.execute(new Runnable()
14             {
15                 public void run()
16                 {
17                     System.out.println(Thread.currentThread().getName() + "-" + taskName);
18                 }
19             });
20         }
21     }
22 }

输出结果:

pool-1-thread-2-task 1
pool-1-thread-1-task 0
pool-1-thread-3-task 2
pool-1-thread-2-task 3
pool-1-thread-1-task 4
pool-1-thread-2-task 6
pool-1-thread-3-task 5
pool-1-thread-2-task 8
pool-1-thread-1-task 7
pool-1-thread-3-task 9

java 5 线程池

原文:http://www.cnblogs.com/java-koma/p/4052638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!