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

线程池

时间:2016-11-18 06:47:31      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:color   runnable   new   cache   ble   int   rgs   import   str   

 

 1 package threads;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 
 6 public class ThreadPoolTest {
 7     public static void Fixed() {
 8         ExecutorService pool = Executors.newFixedThreadPool(6);
 9         Runnable target = new Runnable() {
10             @Override
11             public void run() {
12                 // TODO Auto-generated method stub
13                 for (int i=0; i<5; i++) {
14                     System.out.println(Thread.currentThread().getName()+" 的 i 值为 " + i);
15                 }
16             }
17             
18         };
19         pool.submit(target);
20         pool.submit(target);
21         pool.shutdown();
22     }
23     
24     public static void Cached() {
25         ExecutorService pool = Executors.newCachedThreadPool();
26         Runnable target = new Runnable() {
27             @Override
28             public void run() {
29                 for (int i=0; i<5; i++) {
30                     System.out.println(Thread.currentThread().getName()+" 的 i 值为 " + i);
31                 }
32             }
33             
34         };
35         pool.submit(target);
36         pool.submit(target);
37         pool.shutdown();
38     }
39     
40     public static void main(String[] args) {
41         //ThreadPoolTest.Fixed();
42         ThreadPoolTest.Cached();
43     }
44 }

 

线程池

标签:color   runnable   new   cache   ble   int   rgs   import   str   

原文地址:http://www.cnblogs.com/fysola/p/6076118.html

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