public class LiftOffThread implements Runnable {
protected int countDown = 10;
private static int taskCount = 0;
private final int id = taskCount++;
public LiftOffThread() {
}
public String status() {
return "#" + id + "(" + (countDown > 0 ? countDown : "LiftOff") + ")";
}
@Override
public void run() {
while (countDown-- > 0) {
System.out.println(status());
}
}
}
ExecutorService mService = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
mService.execute(new LiftOffThread());
}
mService.shutdown();
运行结果:
//预先分配线程数
ExecutorService mService = Executors.newFixedThreadPool(3);
for (int i = 0; i < 5; i++) {
mService.execute(new LiftOffThread());
}
mService.shutdown();
运行结果:
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/pengkv/article/details/46833173