{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" One 、 Thread pool construction "}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The use of thread pool is inseparable from ThreadPoolExecutor class , This class implements ExecutorService Interface , Its construction method is as follows :"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"public ThreadPoolExecutor(int corePoolSize,\n\tint maximumPoolSize,\n\tlong keepAliveTime,\n\tTimeUnit unit,\n\tBlockingQueue workQueue,\n\tThreadFactory threadFactory,\n\tRejectedExecutionHandler handler);1"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The parameters are described as follows :"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"corePoolSize: Core pool size "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"maximumPoolSize: Thread pool size (maximumPoolSize >= corePoolSize)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"keepAliveTime: The lifetime of a thread without a task , By default, only the number of threads is greater than corePoolSize when , This parameter only works , If the number of threads equals corePoolSize, Then these threads will survive forever . But if you call allowCoreThreadTimeOut(boolean) Method , Then the number of threads is not greater than corePoolSize when , This parameter also works , Until the number of threads is 0"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"unit:keepAliveTime Time unit of , There are seven values :"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TimeUnit.DAYS;// God "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TimeUnit.HOURS;// Hours "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TimeUnit.MINUTES;// minute "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TimeUnit.SECONDS;// second "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TimeUnit.MILLISECONDS;// millisecond "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TimeUnit.MICROSECONDS;// Microsecond "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TimeUnit.NANOSECONDS;// nanosecond "}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"workQueue: Specifies the blocking queue that makes up the buffer , That is, the queue policy of thread pool is specified , There are three commonly used :"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ArrayBlockingQueue: Array based fifo queue , The size must be specified when this queue is created "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"LinkedBlockingQueue: Fifo queue based on linked list , If this queue size is not specified when created , The default is Integer.MAX_VALUE"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"synchronousQueue: This queue does not hold submitted tasks , Instead, a new thread will be created directly to perform the new task "}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"threadFactory( Optional ): Thread factory , Used to create threads , Customizable "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"handler( Optional ): Refusal strategy , There are four strategies to choose from :"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ThreadPoolExecutor.AbortPolicy: Discard the task and throw it out RejectedExecutionException abnormal ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ThreadPoolExecutor.DiscardPolicy: Discarding the task , Don't throw exceptions ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ThreadPoolExecutor.DiscardOldestPolicy: Discard the top task in the queue , And then try the task again "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ThreadPoolExecutor.CallerRunsPolicy: The calling thread processes the task "}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" Two 、 Running process of thread pool "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" If the number of threads in the current thread pool is less than corePoolSize, Then every task , A thread will be created to perform the task if the number of threads in the current thread pool >=corePoolSize, Then every task , Will try to add it to the task cache queue , If the addition is successful , Then the task will wait for the idle thread to fetch it out for execution ; If the addition fails ( Generally speaking, the task cache queue is full ), Will try to create a new thread to perform this task "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" If the number of threads in the current thread pool reaches maximumPoolSize, Then the task rejection strategy will be adopted to deal with "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" If the number of threads in the thread pool is greater than "},{"type":"text","marks":[{"type":"strong"}],"text":"corePoolSize"},{"type":"text","text":" when , If a thread is idle for more than keepAliveTime, The thread will be terminated , Until the number of threads in the thread pool is no more than corePoolSize;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" If it is allowed to set the lifetime for threads in the core pool ( call "},{"type":"codeinline","content":[{"type":"text","text":"allowCoreThreadTimeOut(boolean)"}]},{"type":"text","text":" Method ), Then the threads in the core pool are idle for more than keepAliveTime, The thread will also be terminated "}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" 3、 ... and 、 Thread initialization "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" By default , There are no threads after the thread pool construction is completed , The thread will not be created until the task is submitted , If you need to create threads when thread pool construction is complete , You can call the following two methods :"}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"prestartCoreThread(): Initializes a core thread ;"}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"prestartAllCoreThreads(): Initializes all core threads "}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" Four 、 Thread pool closure "}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"shutdown(): The thread pool will not be closed immediately , Instead of taking on new tasks , Close the thread pool after all current tasks are processed "}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"shutdownNow(): Close thread pool now , Interrupt the task being performed , Empty buffer queue , Returns a task that has not yet been performed "}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" 5、 ... and 、 Task submitted "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" There are two ways to submit tasks ,execute() and submit()"}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"void execute(Runnable task), No return value "}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"Futuresubmit(Runnable task, T result) /Futuresubmit(Callable task), There is a return value "}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" 6、 ... and 、 Optional thread pool model (Executors Static factory method for class )"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"newCachedThreadPool():"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ThreadPoolExecutor(0,Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue())"}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"newFixedThreadPool(int nThreads):"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ThreadPoolExecutor(nThreads,nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue())"}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"newSingleThreadExecutor:"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ThreadPoolExecutor(1,1,0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue())"}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"newScheduledThreadPool(int nThreads):"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ScheduledThreadPoolExecutor(nThreads,Integer.MAX_VALUE,0L,TimeUnit.NANOSECONDS,new DelayedWorkQueue())"}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"newSingleScheduledThreadPool():"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"newScheduledThreadPool(1)"}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":" Call the instance :"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ExecutorService pool = Executors.newCachedThreadPool();"}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The thread pool model is preferred when constructing thread pool , If these models don't meet the requirements , Customize it again ThreadPoolExecutor Thread pool "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}