idea principal: Se ejecutan un total de 20 tareas, el número de subprocesos principales es 4, el número máximo de subprocesos principales es 10, actualmente se agregan 20 ejecutables (equivalente a 20 tareas),
Es necesario ejecutar 20 tareas, pero el número de subprocesos principales es solo 4 y hay 16 tareas. Dado que la cola De LinkedBlockingQueue es
El número máximo de tareas que se pueden almacenar es 10. Cuando la cola esté llena, se creará un nuevo subproceso para ejecutar la tarea. En este momento, el número máximo de subprocesos es 10.
Todavía hay 6 LinkedBlockingQueues no básicos. En este momento, se abrirán 6 subprocesos para ejecutarse. Actualmente, el número máximo de subprocesos es 10.
Todavía hay 10 en la cola en este momento. Sólo tiene que cumplir con el tamaño de la cola (Almacene primero en la cola De LinkedBlockingQueue, almacene el resto para crear un nuevo subproceso)
package com.zkdj.urlCheck.spring_boot_1.main.java.controller.thread;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import lombok.NonNull;
public class ThreadController {
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final int CORE_POOL_SIZE = Math.max(4, Math.min(CPU_COUNT - 1, 5));
private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 2;
private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<>(10);
private static ThreadPoolExecutor THREAD_POOL_EXECUTOR;
//A total of 20 tasks are executed, the number of core threads is 4, the maximum number of core threads is 10, currently 20 runnables are added (equivalent to 20 tasks),
//20 tasks need to be executed, but the number of core threads is only 4, and there are 16 tasks, because the LinkedBlockingQueue queue is
//The maximum number of tasks stored is 10. When the queue is full, a new thread will be created to execute the task. At this time, the maximum thread is 10.
//There are still 6 non-core LinkedBlockingQueues. At this time, 6 threads will be opened to execute. Currently, the maximum number of threads is 10,
//There are still 10 in the queue at this time. Just meet the size of the queue
static {
System.out.println("Core thread count=" + CORE_POOL_SIZE);
System.out.println("Maximum number of threads=" + MAXIMUM_POOL_SIZE);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
CORE_POOL_SIZE, //Number of core threads
MAXIMUM_POOL_SIZE, //The maximum number of threads in the thread pool
60, //The survival time of the thread, when there is nothing to do, the idle time
TimeUnit.SECONDS, //Unit of thread survival time
sPoolWorkQueue, //Thread cache queue
new ThreadFactory() {//Thread creation factory, if the thread pool needs to create a thread, newThread will be called to create it
@Override
public Thread newThread(@NonNull Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(false);
return thread;
}
});
threadPoolExecutor.allowCoreThreadTimeOut(true);
THREAD_POOL_EXECUTOR = threadPoolExecutor;
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Execution completed" + Thread.currentThread().getName());
}
};
//Throw it to the thread pool for execution
THREAD_POOL_EXECUTOR.execute(runnable);
}
}
}
.