自定义一个线程池

it2022-05-05  168

自定义一个线程池


public class Test2 { public static void main(String[] args) { ExecutorService service = new ThreadPoolExecutor( 2, //corePoolSize 5, //maximumPoolSize 1L, //keepAliveTime TimeUnit.SECONDS, new LinkedBlockingQueue(3), //阻塞队列 Executors.defaultThreadFactory(), //线程池工程 new ThreadPoolExecutor.DiscardOldestPolicy()); //拒绝策略 for (int i = 1; i <=10 ; i++) { final int a = i; service.execute(()->{ System.out.println(Thread.currentThread().getName()+"正在执行业务"+a); }); } } }

输出

pool-1-thread-1正在执行业务1 pool-1-thread-3正在执行业务6 pool-1-thread-2正在执行业务2 pool-1-thread-4正在执行业务7 pool-1-thread-2正在执行业务10 pool-1-thread-1正在执行业务5 pool-1-thread-3正在执行业务9 pool-1-thread-5正在执行业务8

最新回复(0)