标签:pool tac cep test imp 基本 ali ide task
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * 线程池测试类 */ public class TestThreadPool { public static void main(String[] args) { // 实例化线程池对象 corePoolSize--线程池的基本大小,maximumPoolSize--允许的最大线程数 // KeepAliveTime 线程空闲时间 // TimeUnit.MILLISECONDS 线程默认等待时间 // new ArrayBlockingQueue<Runnable>(5)--阻塞队列,阻塞值为5 ThreadPoolExecutor pool = new ThreadPoolExecutor(5,10,200, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5)); for (int i = 0; i < 14; i++){ MyTask task = new MyTask(i); pool.execute(task); System.out.println("线程池中线程数目:"+pool.getPoolSize()+",队列中等待执行的任务数目:"+ pool.getQueue().size()+",已执行完别的任务数目:"+pool.getCompletedTaskCount()); } } } /** * 自定义线程类 */ class MyTask implements Runnable{ private int num; public MyTask(int num){ this.num = num; } @Override public void run() { System.out.println("正在执行任务:" + num); try { Thread.currentThread().sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("任务" + num + "执行完毕"); } }
标签:pool tac cep test imp 基本 ali ide task
原文地址:https://www.cnblogs.com/givre-foudre/p/12343544.html