首页 > 代码库 > 线程池

线程池

 

 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 }

 

线程池