package com.bjsxt.height.concurrent018; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; public class UseThreadPoolExecutor2 implements Runnable{ private static AtomicInteger count = new AtomicInteger(0); @Override public void run() { try { int temp = count.incrementAndGet(); System.out.println("任务" + temp); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace (); } } public static void main(String[] args) throws Exception{ //System.out.println(Runtime.getRuntime().availableProcessors()); BlockingQueue<Runnable> queue = //new LinkedBlockingQueue<Runnable>(); new ArrayBlockingQueue<Runnable>(15); //Bounded queue execution process: 20 threads, execute five first (because 5 are initialized), and then judge whether the queue can fit the remaining 15, if not //15, then create 5 more threads (because Max, created five before, so you can create five more, so it is 10), if you can put it down, the Max parameter is invalid, and it is executed every time //5 threads. The remaining threads can only be placed in the queue. ExecutorService executor = new ThreadPoolExecutor( 5, //core 10, //max 120L, //2fenzhong TimeUnit.SECONDS, queue); for(int i = 0 ; i < 20; i++){ executor.execute(new UseThreadPoolExecutor2()); } Thread.sleep(1000); System.out.println("queue size:" + queue.size()); //10 Thread.sleep(2000); } }