使用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);
}
}
******未经允许,禁止转载 否则追究法律责任******
浙公网安备 33010602011771号