使用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     }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM