java 5 线程池

 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         }
threadPool.shutdown();
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

posted on 2014-10-26 20:07  TroyZ  阅读(151)  评论(0编辑  收藏  举报