"thread pool?"
How to design a dynamic size thread pool, what are the methods? "
thread pool: as the name implies, Create a number of executable threads in a pool (container) beforehand. Getting threads from the pool when needed does not have to be created on their own, so it is not necessary to destroy the threads and put them back in the pool , thereby reducing the overhead of creating and destroying the thread objects .
The cost of starting a new thread is relatively high because it involves interacting with the operating system. At this point, using the thread pool can improve performance, especially if the program When you need to create a large number of short-lived threads , you should consider using a thread pool.
Similar to database connection pool, runnable callable run () or call () method, when run () or call () After the method execution is finished, The thread does not die, but returns to the thread pool again to become idle, waiting to execute the next runnable object run () or call () method.
The thread pool is used to effectively control the number of concurrent threads in the system, and when the system contains a large number of concurrent threads, it can cause severe system degradation and even a JVM crash, while the thread pool's maximum number of threads parameter You can control how many concurrent threads in the system do not exceed this number.
Design a dynamic size of the thread pool, how to design, what should be the method?
A thread pool consists of four basic parts:
1 thread Manager (ThreadPool): Used to create and manage a thread pool, including creating threads, destroying the thread pool, and adding new tasks.
2 worker thread (poolworker): Threads in the thread pool, waiting when there is no task, can perform tasks in a loop.
3 Task Interface : Each task must implement an interface for the worker to schedule the execution of the task, it mainly specifies the entry of the task, the completion of the task after the end of work, the execution status of the task.
4 Task Queue (taskqueue): Provides a buffering mechanism for storing tasks that are not processed.
The methods included:
Private ThreadPool () Create thread pool
public static ThreadPool Getthreadpool () gets a thread pool with the number of default threads
public void Ececute (Runnable Task) performs a task by simply joining the task queue and when execution is determined by the thread pool manager
public void execute (runnable[] Task) executes the task in bulk, but simply joins the task queue and when execution is determined by the thread pool manager
public void Destroy () destroys the thread pool, which ensures that all threads are destroyed if all tasks are completed, otherwise it is not destroyed until the task is completed
public int getwork threadnumber () returns the number of worker threads
public int Getfinishedtasknumber () returns the number of tasks that have been completed, where the task queue is indicated by the number of tasks that may be changed without actual execution completion
public void Addthread () increases the number of threads in the thread pool when all threads in the thread pool are guaranteed to be executing and the number of threads to execute is greater than a certain value
public void Reducethread () reduces the number of threads in the thread pool when the thread in the thread pool is guaranteed to have a large portion of the thread in an idle state and the idle state is less than a certain value
Thread pool? How to design a dynamic size thread pool, what are the methods?