java 写一段动态线程池案例
时间: 2023-03-11 15:01:36 浏览: 127
java上机案例、题目
例如:import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class DynamicThreadPool {
public static void main (String[] args) {
// 创建一个可以根据需要创建新线程的线程池
ExecutorService executor = Executors.newCachedThreadPool();
// 向线程池提交10个任务
for (int i = 0; i < 10; i++) {
Runnable worker = new WorkerThread("" + i);
executor.execute(worker);
}
// 关闭线程池
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
}
}
阅读全文