使用newFixedThreadPool创建线程


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestThread {
// 使用newFixedThreadPool创建线程
private ExecutorService fixedPool = Executors.newFixedThreadPool(5);

public static void main(String[] args) {
TestThread t = new TestThread();
t.rr();
}

public void rr() {
for (int i = 0; i < 20; i++) {
fixedPool.execute(new MyThread03(i));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

class MyThread03 implements Runnable {
int num;

public MyThread03(int num) {
super();
this.num = num;
}

@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":" + num);

}
}

posted on 2019-05-24 13:53  花非花-雾非雾  阅读(555)  评论(1)    收藏  举报