当前位置:网站首页>What is a thread pool?

What is a thread pool?

2022-06-11 05:42:00 Zippx.

In object oriented programming Creating and destroying objects is time consuming Because an object is created to obtain memory resources or other resources stay java Especially in China The virtual machine keeps the view track of each object In order to be able to garbage collection after the object is destroyed Therefore, one way to improve service efficiency is to reduce the number of creation and destruction of objects as much as possible In particular, some resource objects are destroyed This is the reason why pooled resource technology comes into being

 

As the name suggests, thread pool is to create several executable threads in advance and put them into a pool When needed, get threads from the pool without creating them There is no need to destroy the thread after use Instead, it returns to the pool, reducing the overhead of creation and destruction

 

A thread pool consists of four basic components

1. Thread pool manager (ThrredPool)

Used to create and manage thread pools Including creating thread pools Destroy thread pool Add a new task

2. Worker pool (PoolWorker)

Threads in thread pool Waiting when there is no task You can cycle tasks

3. Task interface (Task)

The interface that each character must implement Implementation of tasks scheduled by worker threads It mainly specifies the entry of tasks The closing work after the implementation of the task Task execution status, etc

4. Task queue (TaskQuery)

①newSingleThreadExecutor

The thread pool of a single thread , That is, only one thread works in the thread pool at a time , Single thread serial execution task

②newFixedThreadExecutor(n)

A fixed number of thread pools , Not submitting a task is a thread , Until the maximum number of thread pools , Then it enters the waiting queue until the previous task is completed

③newCacheThreadExecutor( Recommended )

Cacheable thread pool , When the thread pool size exceeds the number of threads needed to process the task , Then it will recycle some of the idle ( It's usually 60 No execution per second ) The thread of , When a mission comes , And intelligently add new threads to execute .

④newScheduleThreadExecutor

Unlimited thread pool , Supports timed and periodic execution threads

 

原网站

版权声明
本文为[Zippx.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020537212973.html