写一个java线程池代码
时间: 2023-03-13 17:07:00 浏览: 111
Java手写线程池的实现方法
Java线程池代码的例子如下:import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadPoolExample {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5); 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");
}
}
阅读全文