newFixedThreadPool固定线程使用

1.newFixedThreadPool固定线程池,  使用完毕必须手动关闭线程池, 否则会一直在内存中存在,

 1 package ThreadTest;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 
 6 public class Demo01 {
 7     public static void main(String[] args) {
 8         MyThread myThread = new MyThread();
 9         ExecutorService pool = Executors.newFixedThreadPool(4);
10         for(int i=0; i<40; i++) {
11             pool.execute(myThread);
12         }
13         pool.shutdown();
14     }
15 }
16 
17 class MyThread implements Runnable{
18     @Override
19     public void run() {
20         for(int i=0; i<30; i++) {
21             System.out.println(Thread.currentThread().getName());
22         }
23     }
24     
25 }

 

posted @ 2017-12-19 15:27  林**  阅读(5751)  评论(0编辑  收藏  举报