This is the 18th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Why use a thread pool, and is the more the better?
- Thread is an object in Java, but also a resource of the operating system. It takes time to create and destroy threads. If the create time + destroy time > execute task time is not cost-effective.
- Java objects occupy heap memory, operating system threads occupy system memory, and according to the JVM specification, the default maximum stack size for a thread is 1M, which is allocated from system memory. Too many threads consume too much memory.
- Operating systems need to switch thread contexts frequently (everyone wants to be run), which affects performance.
- Thread pools are designed to control the number of threads conveniently.
Thread pool principles – Concepts
- Thread pool manager: used to create and manage thread pools, including creating thread pools, pin thread pools, adding new tasks;
- Worker thread: a thread in the thread pool that is in a wait state when no task is available and can execute tasks in a loop
- Task interface: the interface that each task must implement for the worker thread to schedule the execution of the task. It mainly specifies the entry to see the task, the finishing work after the task is executed, the execution status of the task, etc
- Task queue: Used to store unprocessed tasks and provide a buffer mechanism.
Thread pool API- Interface definition and implementation classes
Can think ScheduledThreadPoolExecutor is one of the most abundant implementation class
ExecutorService
ScheduledExecutorService
Thread Pool API-Executors Tools
You can instantiate the thread yourself or create the thread pool factory class using Executors by following the following methods:
NewFixedThreadPool (int nThreads) creates a thread pool of fixed size with unbounded queue capacity. Number of core threads = Maximum number of threads.
NewCachedThreadPool () creates an unbounded buffer thread pool. Its task queue is a synchronization queue. The task is added to the pool. If there are free threads in the pool, the task is executed with free threads. If there are no free threads, a new thread is created to execute the task. Threads in the pool that are idle for more than 60 seconds will be destroyed and freed. The number of threads varies with the number of tasks. Suitable for asynchronous tasks with small overhead. The number of core threads in the pool =0 and the maximum number of threads = integer.max_value
NewSingleThreadScheduledExecutor () only one thread to execute a single thread pool of unbounded task queue. This thread pool ensures that the order in which tasks are added is executed one by one. When the only thread aborts due to a task exception, a new thread is created to continue the subsequent task. The difference from newFixedThreadPool is that the pool size of a single thread pool is hard-coded in the newSingleThreadExecutor() method and cannot be changed.
NewScheduledThreadPool (int corePoolSize) Specifies the thread pool that can be timed to recognize tasks. The number of core threads in the pool is specified by the parameter, and the maximum number of threads is = integer.max_value
Thread Pool principle – Task Execute process
- Is the number of core threads reached? If not, create a worker thread to execute the task.
- Is the work queue full? If not, the newly submitted task is stored in the work queue.
- Is the maximum number of thread pools reached? If not, a new worker thread is created to execute the task.
- Finally, execute the reject policy to handle the task.
Number of threads
How do YOU determine the right number of threads
Computational tasks: 1-2 times the number of cpus
IO tasks: Require more threads than computational tasks. This depends on the SPECIFIC I/O blocking duration.
For example, the default maximum number of threads in Tomcat is 200.
You can also consider automatically increasing or decreasing the number of threads between a minimum number and a maximum number as needed.
Code practice
/** * test: * * @param threadPoolExecutor passes in different thread pools, * @throws Exception */ public void testCommon(ThreadPoolExecutor ThreadPoolExecutor) throws InterruptedException { for (int i = 0; i < 15; i++) { int n = i ; ThreadPoolExecutor. Submit (new Runnable () {@ Override public void the run () {try {System. Out. Println (" start..." + n); Thread.sleep(3000L); System.err.println(" end execution............" + n); } catch (InterruptedException e) { e.printStackTrace(); }}}); Thread.sleep(300L); System. The out. Println (" 1, the current thread pool thread number is: "+ threadPoolExecutor. GetPoolSize ()); System. The out. Println (" 1 - waiting for the number of the current thread pool is: "+ threadPoolExecutor. GetQueue (). The size ()); Thread.sleep(15000L); thread.sleep (15000L); System. The out. Println (" 2 - the current thread pool threads number is: "+ threadPoolExecutor. GetPoolSize ()); System. The out. Println (" 2 - waiting for the number of the current thread pool is: "+ threadPoolExecutor. GetQueue (). The size ()); /** * 1, thread pool information: number of core threads 5, maximum number of threads 10, unbounded queue, thread lifetime beyond the number of core threads: 5 seconds, Throws Exception */ public void threadPoolExecutorTest1() throws InterruptedException {ThreadPoolExecutor ThreadPoolExecutor = new threadPoolExecutor (5,10,5, timeunit.seconds, new LinkedBlockingQueue<Runnable>()); testCommon(threadPoolExecutor); // No boundary queue, the core thread is 5, the maximum thread is 10, because the queue has no boundary, the rest of the tasks are thrown into the queue, so the number of core threads is executed each time}Copy the code
Homework after class
- Thread pool information: number of core threads 5, maximum number 10, queue size 3, thread lifetime beyond the number of core threads: 5 seconds, specify reject policy
- Thread pool information: number of core threads 5, maximum number 5, unbounded queue, thread lifetime beyond the number of core threads: 5 seconds
- Number of core threads 0, maximum number integer. MAX_VALUE, SynchronousQueue specifies the lifetime of threads that exceed the number of core threads: 60 seconds
- Number of core threads 5, maximum number integer. MAX_VALUE, DelayedWorkQueue Specifies a delay queue in which the number of threads exceeds the number of core threads: 0 seconds
- Timed execution thread pool information: Fixed number of threads 5, number of core threads 5, maximum number of integer. MAX_VALUE, DelayedWorkQueue Delay queue. The lifetime of threads exceeding the number of core threads is 0 seconds
- Terminated Threads: Thread pool information: Number of core threads 5, maximum number of threads 10, queue size 3, thread lifetime beyond the number of core threads: 5 seconds, specified reject policy
- Thread pool information: Number of core threads 5, maximum number of threads 10, queue size 3, thread lifetime beyond the number of core threads: 5 seconds, specified reject policy