import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class Main { public static void main(String[] args) { // 设置线程池参数 int corePoolSize = 5; // 核心线程数 int maxPoolSize = 10; // 最大线程数 long keepAliveTime = 60; // 线程空闲时间 TimeUnit unit = TimeUnit.SECONDS; // 空闲时间单位 ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10); // 任务队列,这里使用有界队列 // 创建线程池 ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, unit, workQueue); // 提交任务给线程池 for (int i = 0; i < 10; i++) { final int taskId = i; executor.submit(() -> { System.out.println("Task-" + taskId + " is running"); try { Thread.sleep(2000); // 模拟任务执行 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Task-" + taskId + " is finished"); }); } // 关闭线程池 executor.shutdown(); } }
标签:java,int,获取,任务,Future,线程,返回值 From: https://www.cnblogs.com/jiangzishun/p/18182557