可以用同一个线程一一执行多个任务而不破坏和重新创建线程吗?
public class SimpleThreadPool {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
Runnable worker = new WorkerThread("" + i);
executorService.execute(worker); // How many threads are created?
}
executorService.shutdown();
while (!executorService.isTerminated()) {
}
System.out.println("All threads Executed");
}
}
阿波罗的战车
相关分类