package thead.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolNewCached01Use {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();

        // 提交多个任务
        for (int i = 0; i < 1000; i++) {
            final int taskId = i;
            executorService.execute(() -> {
                System.out.println("Task " + taskId + " executed by: " + Thread.currentThread().getName());
                try {
                    Thread.sleep(2000);  // 模拟任务执行
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        // 关闭线程池
        executorService.shutdown();
    }
}
