使用newFixedThreadPool创建线程(可以传入线程的个数)

 1         //使用newFixedThreadPool创建线程
 2         ExecutorService fixedPool = Executors.newFixedThreadPool(5);
 3         for (int i = 0; i <20; i++) {
 4             fixedPool.execute(new MyThread03(i));
 5             try {
 6                 Thread.sleep(1000);
 7             } catch (InterruptedException e) {
 8                 // TODO Auto-generated catch block
 9                 e.printStackTrace();
10             }
11         }
12     }
13 }
14 class MyThread03 implements Runnable{
15     int num;
16 
17     public MyThread03(int num) {
18         super();
19         this.num = num;
20     }
21 
22     @Override
23     public void run() {
24         System.out.println(Thread.currentThread().getName()+":"+num);
25         
26     }

 

posted @ 2018-10-09 18:01  卡多希  阅读(2262)  评论(0)    收藏  举报