码迷,mamicode.com
首页 > 编程语言 > 详细

java 5 线程池

时间:2014-10-26 21:13:35      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   java   for   sp   div   on   

 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 线程池

标签:style   blog   color   ar   java   for   sp   div   on   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!