/** * corePoolSize: number of core threads * maximumPoolSize: maximum number of threads * keepAliveTime: thread idle time * TimeUnit unit: time unit * workQueue: work queue * ThreadFactory: thread factory * RejectedExecutionHandler handler: rejection strategy * AbortPolicy: reject the task and throw the exception * CallerRunsPolicy: executed by the caller * DiscardOldestPolicy: Add new tasks and discard the oldest tasks * DiscardPolicy: Discard directly, do nothing * * Workflow of thread pool * 1. At the beginning, the thread pool is empty * 2. With the submission of tasks, start to create threads * 1>if (current number of threads < corePoolSize), create threads * 2>if (current number of threads = corePoolSize), add tasks to the work queue * 3> The queue is full, if (current thread < maxPoolSize), create a thread * 4> The queue is full, if (the current number of threads == maxPoolSize), execute the rejection strategy * 3. With the execution of the task, the primary key of the remaining task is reduced, and there are gradually idle threads * if (idle time > keepAliveTime, and the current number of threads > corePoolSize), destroy the thread, knowing that the current number of threads == corePoolSize * * Common thread pool:
FixedThreadPool | The number of core threads is the same as the maximum number of threads |
SingleThreadExecutor | a thread pool |
CachedThreadPool | The core thread is 0, and the maximum number of threads is Integer. MAX_VALUE |
ScheduledThreadPool | Timing thread pool with specified number of core threads |
SingleThreadScheduledExecutor | Singleton timing thread pool |
ForkJoinPool | A thread pool newly added in JDK 7 |
* * Create thread pool yourself: * 1. Thread * 2. There is a container for saving threads * 3. Tasks * 4. Queue * 5. Need to provide a method to add tasks to the thread pool */ import java.util.ArrayList; import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; class Worker extends Thread{ BlockingQueue<Runnable> queue = null; public Worker(BlockingQueue<Runnable> queue){ this.queue = queue; } public void run() { //Scan the task queue and execute while (true){ try { Runnable runnable = queue.take(); runnable.run(); }catch (Exception e){ e.printStackTrace(); } } } } public class MyThreadPool { BlockingQueue<Runnable> queue = new LinkedBlockingQueue(); List<Worker> workerList = new ArrayList<>(); public MyThreadPool(int corePoolSize){ for (int i = 0; i < corePoolSize; i++) { Worker worker = new Worker(queue); worker.start(); workerList.add(worker); } } public void submit(Runnable runnable){ try { queue.put(runnable); } catch (InterruptedException e) { e.printStackTrace(); } } }
test code
import java.util.Date; public class MyThreadPool_Test { public static void main(String[] args) { MyThreadPool pool = new MyThreadPool(3);//Call the thread pool created by yourself for (int i = 0; i < 10; i++) { pool.submit(() -> { System.out.println("hello" + new Date()); try { // handle exception Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }); } } }