Thread overhead:

A. The overhead of creating and starting threads. Java threads also take up extra storage space compared to normal objects: stack space; Moreover, starting a thread will incur the corresponding thread scheduling overhead

B. Thread destruction

C. Scheduling overhead of threads: scheduling of threads leads to context switching, which increases the consumption of processor resources and reduces the processor resources that can be used by programs

D. The number of threads a system can create is always limited by the number of processors the system has. The threshold is always the number of processors

 

 

public ThreadPoolExecutor(

IntcorePoolSize, // Specifies the thread pool core size

IntmaximumPoolSize, // Specifies the maximum thread pool size

       longkeepAlive,

TimeUnitunit, // With keepAlive indicates the maximum keepAlive time

BolckingQueue<Runnable>workQueue, // the blocking queue of the workQueue, equivalent to the transport channel in the production consumer

ThreadFactorythreadFactory, / / specified create a worker thread thread factory

RejectedExecutionHandlerhandler) / / see the table below in detail

The implementation class

The processing strategy implemented

ThreadPoolExecutor.AbortPolicy

Throw an exception directly

ThreadPoolExecutor.DiscardPolicy

Discards the currently rejected task

ThreadPoolExecutor.DiscardOldestPolicy

Discard the oldest task in the work queue and try again to accept the rejected task

ThreadPoolExecutor.CallerRunsPolicy

Execute the rejected task in the client thread

 

Under the initial state, the client for each task, submit the thread pool creates a worker thread to handle this task, with the increase of the task, the thread pool size will increase, reach the core thread pool size, the new task will be deposited in the job queue, the task of the cache out shall be the responsibility of the worker thread to perform; The thread pool calls the non-blocking offer(E) method of BlockingQueue when it places a task on the work queue. The threadFactory.newThread method is used to create worker threads. If you don’t specify when creating the thread pool thread factory, then will use Executors. DefaultThreadFactory () returns the default thread factory.

When the thread pool is saturated, i.e., when the worker queue is full and the current thread pool size reaches the maximum thread pool size, the client’s attempt to submit the task will be rejected. The processing strategy can be used:

RejectedExecutionHandler interface method:

void rejectedExecution(Runnabler,ThreadPoolExecutor executor)

ThreadPoolExecutor. Shutdown ()/shutdownNow () can be used to close the thread pool

When Shutdown() shuts down the thread pool, submitted tasks are continued and new submitted tasks are rejected as if the pool is saturated

When ShutdownNow() closes the thread pool, tasks that are being executed are closed and submitted tasks that are waiting are not executed

 

Task processing results, exception handling, and cancellation

If the client is interested in the outcome of the task, it can submit the task using ThreadPoolExecutor’s Submit method

public <T> Future<T> submit(Callable<T>task)

The Callable interface is equivalent to an enhanced Runnable interface, defining the only method:

V call() throws Exeception Indicates that an exception can be thrown during task execution

Procedure for transferring the Runnable interface to the Callable: Executors. Callable (Runnable Task, T result)

The future.get () method can be used to retrieve the result of a task specified by the task argument. When called, if the task is not completed, the current thread will be suspended until the task is completed or an exception will be thrown. So the future.get () method is typically called the moment the corresponding task processes the resulting data

Boolean Cancel (Boolean mayInterruptIfRunning)// Whether the task was canceled successfully

Boolean isDone() checks whether the corresponding task is complete

V get (long timeout, the timeout unit) throwsInterruptedExeception, ExecutionExeception, TimeoutExeception / / action and Future. The get () the same

 

Thread pool monitoring

ThreadPoolExecutor provides methods related to thread pool monitoring

methods

use

getPoolSize()

Gets the current thread pool size

getQueue()

Gets the work queue instance

getLargestPoolSize()

Gets the maximum number of worker threads ever reached

getAliveCount()

Gets the number of worker threads executing tasks in the thread pool

getTaskCount()

Gets the number of tasks received by the thread pool

getCompletedTaskCount()

Gets the number of completed tasks

 

 

Thread pool deadlocks

The same thread pool can only be used to execute tasks that are independent of each other. Dependent tasks need to be submitted to different thread pools to avoid deadlocks

 

Abnormal termination of the worker thread

If a task is through the ThreadPoolExecutor. Submit call submitted to the thread pool, task in the process of execution throws an uncaught exception will not lead to abnormal termination of the worker threads of execution

If a task is through the ThreadPoolExecutor. Execute call submitted to the thread pool, task once throw an uncaught exception, for the worker thread of execution will abort