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("Task-" + i); executor.execute(worker); } executor.shutdown(); while (!executor.isTerminated()) { } } } class WorkerThread implements Runnable { private String taskName; public WorkerThread(String taskName) { this.taskName = taskName; } public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }


上一篇:
下一篇:
切换中文