Thread pool (parameters in constructor, execution, rejection policy, shutdown, ThreadLocal)

Thread Pool

No need to create and destroy frequently

Why use thread pool?

When the amount of concurrency is large, frequent creation and destruction of threads is expensive.

Create a thread pool to relieve stress

The meaning of each parameter in the constructor (7)

corePoolSize : The number of core thread pools. After creation, the number of core thread pools defaults to 0. Threads will be created for execution after a task comes, or the prestartAllCoreThreads or prestartCoreThread() method is called for pre-creation.

maximumPoolSize : The total number of thread pools, indicating the maximum number of threads that can be installed in a thread.

unit : defines the time unit for keepAliveTime

keepAliveTime : The value is how long a thread in the non-core thread pool will be idle when no tasks are executed before it is destroyed.

workQueue : Waiting queue, you can specify the implementation class of the waiting queue yourself

threadFactory : Thread factory is mainly used to create threads

handler : indicates the rejection strategy when handling rejected tasks

Thread pool execution

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-lRodbdDF-1642513624398) (C:\Users\Cloud\AppData\Roaming\Typora\typora-user-images\ 1642488542146.png)]

When a task is submitted, first check whether the core thread pool is full. If it is not full: create a thread in the core thread pool for processing. If it is full: add the task to the waiting queue.

Tasks continue to be submitted, the core thread pool is full, and the waiting queue is full. At this time, non-core threads must be created to process the tasks.

If the number of tasks continues to increase and the core thread pool, waiting list, and non-core thread pool are all full, then the corresponding rejection policy will be used.

Thread pool rejection policy

AbortPolicy policy: throw exception

iscardOleddestPolicy policy: This policy will discard the oldest request

DiscardPolicy policy: This policy discards tasks that cannot be processed without any processing.

CallerRunsPolicy: Let the thread that submitted the task execute it, such as our main thread

The difference between execute and submit

Both execute and submit submit tasks to the thread pool. Their main differences are:

execute has no return value

submit can receive return values

Thread pool shut down

Closing the thread pool can be achieved by calling the shutdownNow and shutdown methods.

shutdownNow: Issue interrupt() to all tasks that are being executed, stop execution, cancel all tasks that have not yet started, and return the list of tasks that have not yet started. That is, the thread task is terminated immediately.

shutdown: When we call shutdown, the thread pool will no longer accept new tasks, but it will not forcefully terminate tasks that have been submitted or are being executed. That is, no new tasks will be accepted and the task will be closed after execution.

How to create a thread:

1. Inherit the Thread class 2. Implement the Runnable interface 3. Implement the Callable interface 4. Use the thread pool

ThreadLocal

ThreadLocal is called a thread variable.

This means that the variable filled in ThreadLocal belongs to the current thread, and the variable is isolated from other threads. ThreadLocal

Save a copy of variables for each thread so that variables between multiple threads do not affect each other.

ThreadLocal principle analysis

Create a ThreadLocal object

Calling the set method will obtain the currently executing thread object at the bottom and create a ThreadLocalMap object for our current thread.

The key of ThreadLocalMap is the ThreadLocal object, and the value is the value of our own set

When searching, first find the ThreadLocalMap of the object through our own thread.

Thread t = Thread.currentThread();

ThreadLocalMap map= getMap(t);

ThreadLocal memory leak problem

Memory leak: Some objects are no longer used in memory, but cannot be recycled.

The key of ThreadLocalMap is ThreadLocal, which is managed by a weak reference object. If the thread is executed for a long time, then the key is a weak reference, and the key is recycled and is null, but the value may still be occupied and is a strong reference. , cannot be recycled and may lead to memory leaks.

Solution: Delete the variables in ThreadLocal immediately after they are used.

Guess you like

Origin blog.csdn.net/crraxx/article/details/122569486