In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you what is the implementation principle of thread pool, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Why use thread pools?
Is it better to have more threads?
Thread is not only an object in Java, but also a resource of the operating system. It takes time to create and destroy threads. It is not cost-effective if creation time + destroy time > task execution time
Java objects occupy heap memory and operating system threads occupy system memory. According to the JVM specification, the default maximum stack size of a thread is 1m. This stack space is allocated from operating system memory. Too many threads will consume a lot of memory.
Frequent thread context switching by the operating system can affect performance
The thread pool is launched to control the number of threads.
Thread pool principle-concept
Thread Pool Manager:
Used to create and manage thread pools, including creating thread pools, destroying thread pools, and adding new tasks
Worker thread:
Threads in the thread pool, which are waiting when there is no task, can execute the task in a loop
Task interface:
The interface that each task must implement for the execution of the worker thread task scheduling, which mainly defines the entry of the task, the closing work after the task execution, the execution status of the task, and so on.
Task queue:
Used to store unprocessed tasks, providing a buffer
Thread pool API-interface definition and implementation class
Thread pool API-method definition
ExecutorService
CheduledExecutorService
Thread pool API-Executors utility class
You can instantiate a thread pool yourself, or you can create a thread pool with Executors as follows:
NewFixedThreadPool (int nThreads): create a fixed size thread pool with unbounded task queues, core threads = maximum number of threads
NewCachedThreadPool (): create a buffer thread pool of unlimited size, whose task queue is a synchronous queue, and tasks are added to the pool. If there are idle threads in the pool, they will be executed with idle threads. If there are none, new threads will be created for execution. Idle threads in the pool will be destroyed and released for more than 60 seconds. The number of threads varies with the number of tasks. It is suitable for less time-consuming asynchronous tasks. The number of core threads in the pool = 0. Maximum number of threads = Integer.MAX_VALUE
NewSingleThreadExecutor (): a single thread pool in which only one thread executes an unbounded task queue, which ensures that tasks are executed one by one in the order in which they are joined. When a unique thread aborts abnormally due to a task, a new thread is created to continue to execute subsequent tasks. Unlike newFixedThreadPool (1), the pool size of a single thread pool is hard-coded in the newSingleThreadExecutor method and cannot be changed.
NewScheduledThreadPool (int corePoolSize): a pool of threads that can execute tasks on a regular basis. The number of core threads in this pool is specified by the parameter. The maximum number of threads = Integer.MAX_VALUE
Thread pool principle-task execution process
Has the number of core threads been reached?
Do not reach, create a worker thread to execute the task
Is the work queue full?
If it is not full, the newly submitted task is stored in the task queue
Has the maximum number of thread pools been reached?
A new thread is created to execute the task
Finally, execute the reject policy to handle the task
Code example:
Package com.neteasy.demo
Import java.util.List
Import java.util.concurrent.*
/ * * use of thread pool * /
Public class Demo7 {
/ * *
* Test: submit 15 tasks that take 3 seconds to execute, depending on the status of the thread pool
*
* @ param threadPoolExecutor
* @ throws Exception
, /
Private void testCommon (ThreadPoolExecutor threadPoolExecutor) throws Exception {
/ / Test: submit 15 tasks that take 3 seconds to execute. See the processing of 2 tasks that exceed the size.
For (int I = 0; I < 15; iTunes +) {
Int n = I
ThreadPoolExecutor.submit (
New Runnable () {
@ Override
Public void run () {
Try {
System.out.println ("start execution:" + n)
Thread.sleep (3000L)
System.out.println ("end of execution:" + n)
} catch (InterruptedException e) {
E.printStackTrace ()
}
}
});
System.out.println ("Task submitted successfully:" + I)
}
/ / check the number of threads and the number of queue waits
Thread.sleep (500L)
System.out.println ("number of current thread pool threads is:" + threadPoolExecutor.getPoolSize ())
System.out.println ("number of current thread pool waits" + threadPoolExecutor.getQueue () .size ())
/ / wait 15 seconds to check the number of threads and queues (in theory, threads that exceed the number of core threads will be automatically destroyed)
Thread.sleep (15000L)
System.out.println ("number of current thread pool threads is:" + threadPoolExecutor.getPoolSize ())
System.out.println ("the number of current thread pool waits is:" + threadPoolExecutor.getQueue () .size ())
}
/ * *
* 1. Thread pool information: the number of core threads is 5, the maximum number is 10, the unbounded queue, the thread survival time beyond the number of core threads: 5 seconds, specify the rejection policy of
*
* @ throws Exception
, /
Private void threadPoolExecutorTest1 () throws Exception {
ThreadPoolExecutor threadPoolExecutor =
New ThreadPoolExecutor (5,10,5, TimeUnit.SECONDS, new LinkedBlockingDeque ())
TestCommon (threadPoolExecutor)
/ / expected result: the number of threads in the thread pool is: 5, the number of tasks exceeds, and the others enter the queue waiting to be executed
}
/ * *
* 2. Thread pool information: the number of core threads is 5, the maximum number of threads is 10, the queue size is 3, and the survival time of threads exceeding the number of core threads is 5 seconds.
*
* @ throws Exception
, /
Private void threadPoolExecutorTest2 () throws Exception {
/ / create a thread pool with a maximum of 10 core threads and a maximum waiting queue of 3, that is, a maximum of 13 tasks.
/ / the default policy is to throw a RejectedExecutionException exception, java.util.concurrent.ThreadPoolExecutor.AbortPolicy
ThreadPoolExecutor threadPoolExecutor =
New ThreadPoolExecutor (
five,
ten,
five,
TimeUnit.SECONDS
New LinkedBlockingDeque (3)
New RejectedExecutionHandler () {
@ Override
Public void rejectedExecution (Runnable r, ThreadPoolExecutor executor) {
System.err.println ("some tasks have been denied")
}
});
TestCommon (threadPoolExecutor)
/ / expected result:
/ / 1. 5 tasks are directly assigned to threads to start execution.
/ / 2, 3 tasks enter the waiting queue
/ / 3. If the queue is not enough, add 5 threads temporarily to execute the task (destroy if there is no work for 5 seconds)
/ / 4, the queue and thread pool are full, and the remaining 2 tasks are rejected because they have no resources.
/ / 5. Task execution. After 5 seconds, if there is no task to execute, destroy the 5 threads created temporarily.
}
/ * *
* 3. Thread pool information: number of core threads 5, maximum number 5, unbounded queue, thread survival time beyond the number of core threads: 5 seconds
*
* @ throws Exception
, /
Private void threadPoolExecutorTest3 () throws Exception {
/ / same as Executors.newFixedThreadPool (int nThreads)
ThreadPoolExecutor threadPoolExecutor =
New ThreadPoolExecutor (
5,5,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue ()
TestCommon (threadPoolExecutor)
/ / expected knot: the number of threads in the thread pool is: 5, the number of tasks exceeds the number, and the others enter the queue waiting to be executed
}
/ * *
* 4. Thread pool information: the number of core threads is 0, the maximum number of Integer.MAX_VALUE,SynchronousQueue queues, and the thread survival time beyond the number of core threads: 60 seconds
*
* @ throws Exception
, /
Private void threadPoolExecutorTest4 () throws Exception {
/ * *
* SynchronousQueue, which is not really a queue because it does not maintain storage space for the elements in the queue. Unlike other queues, it maintains a set of threads waiting for elements to be added to or removed from the queue.
* under the premise of using SynchronousQueue as the work queue, when the client code submits a task to the thread pool, and no idle thread in the thread pool can fetch a task from the SynchronousQueue queue instance
* then the corresponding offer method call will fail (that is, the task is not stored in the work queue). At this point, ThreadPoolExecutor will create a new worker thread to handle the task that failed to queue.
* (assuming that the thread pool size has not yet reached its maximum thread pool size maximumPoolSize).
, /
/ / same as Executors.newCachedThreadPool ()
ThreadPoolExecutor threadPoolExecutor =
New ThreadPoolExecutor (
0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue ()
TestCommon (threadPoolExecutor)
/ / expected result:
/ / 1. The number of threads in the thread pool is 15. If the number of tasks exceeds, others enter the queue waiting to be executed.
/ / 2. All tasks are finished. After 60 seconds, if there are no tasks to execute, all threads are destroyed and the pool size is restored to 0.
Thread.sleep (6000L)
System.out.println ("60 seconds later, look at the number of threads in the pool:" + threadPoolExecutor.getPoolSize ())
}
/ * *
* 5. Regular execution thread pool information: execute in 3 seconds, one-time task, and execute at the right time.
* the number of core threads is 5, and the maximum number of Integer.MAX_VALUE,DelayedWorkQueue delay queues. The thread survival time beyond the number of core threads is 0 seconds.
*
* @ throws Exception
, /
Private void threadPoolExecutorTest5 () throws Exception {
/ / same as Executors.newScheduledThreadPool ()
ScheduledThreadPoolExecutor threadPoolExecutor = new ScheduledThreadPoolExecutor (5)
ThreadPoolExecutor.schedule (
New Runnable () {
@ Override
Public void run () {
System.out.println ("Task executed, now time:" + System.currentTimeMillis ())
}
}
3000
TimeUnit.MILLISECONDS)
System.out.println (
"scheduled task, submitted successfully, the time is:"
+ System.currentTimeMillis ()
+ ", number of threads in the current thread pool:"
+ threadPoolExecutor.getPoolSize ()
/ / expected result: the task is executed once after 3 seconds
}
/ * *
* 6. Timed execution thread pool information: fixed number of threads 5
* the number of core threads is 5, and the maximum number of Integer.MAX_VALUE,DelayedWorkQueue delay queues. The thread survival time beyond the number of core threads is 0 seconds.
*
* @ throws Exception
, /
Private void threadPoolExecutorTest6 () throws Exception {
ScheduledThreadPoolExecutor threadPoolExecutor = new ScheduledThreadPoolExecutor (5)
/ * *
* perform a task periodically. Thread pool provides two scheduling methods, which are demonstrated separately here. The test scenario is the same. Test scenario: the submitted task takes 3 seconds to complete. Look at the difference between the two different scheduling methods 1:
* after submission, the first execution starts after 2 seconds, and then it is executed regularly every 1 second (if it is found that the last execution is not finished, the wait is over, and execute immediately after the completion).
* that is to say, the code is executed every 3 seconds (calculated: each execution is performed for three seconds, with an interval of 1 second, and the next execution starts immediately after the execution ends, without waiting)
, /
ThreadPoolExecutor.scheduleAtFixedRate (
New Runnable () {
@ Override
Public void run () {
Try {
Thread.sleep (3000L)
} catch (InterruptedException e) {
E.printStackTrace ()
}
System.out.println ("Task-1 was executed, now time:" + System.currentTimeMillis ())
}
}
2000
one thousand,
TimeUnit.MILLISECONDS)
/ * *
* effect 2: after the submission, the first execution starts after 2 seconds, and then it is executed regularly every 1 second (if it is found that the last execution has not been completed, the waiting is finished, and the timing starts after the last execution, waiting for 1 second).
* in other words, the effect of this code clock is to be executed every 4 seconds. (calculation method: 3 seconds for each execution, 1 second interval, and then wait 1 second after execution, so it is 3 seconds 1)
, /
ThreadPoolExecutor.scheduleWithFixedDelay (
New Runnable () {
@ Override
Public void run () {
Try {
Thread.sleep (3000L)
} catch (InterruptedException e) {
E.printStackTrace ()
}
System.out.println ("Task-2 is executed, now time:" + System.currentTimeMillis ())
}
}
2000
one thousand,
TimeUnit.MILLISECONDS)
}
Private ThreadPoolExecutor testShutdownCommon () {
/ / create a thread pool with a maximum of 10 core threads and a maximum waiting queue of 3, that is, a maximum of 13 tasks.
/ / the default policy is to throw a RejectedExecutionException exception, java.util.concurrent.ThreadPoolExecutor.AbortPolicy
ThreadPoolExecutor threadPoolExecutor =
New ThreadPoolExecutor (
five,
ten,
five,
TimeUnit.SECONDS
New LinkedBlockingQueue (3)
New RejectedExecutionHandler () {
@ Override
Public void rejectedExecution (Runnable r, ThreadPoolExecutor executor) {
System.err.println ("some tasks have been denied")
}
});
/ / Test: submit 15 tasks that take 3 seconds to execute. See the processing of 2 tasks that exceed the size.
For (int I = 0; I < 15; iTunes +) {
Int n = I
ThreadPoolExecutor.submit (
New Runnable () {
@ Override
Public void run () {
Try {
System.out.println ("start execution:" + n)
Thread.sleep (3000L)
System.err.println ("end of execution:" + n)
} catch (InterruptedException e) {
System.out.println ("exception:" + e.getMessage ())
}
}
});
System.out.println ("Task submitted successfully:" + I)
}
Return threadPoolExecutor
}
/ * *
* 7. Terminating threads: thread pool information: the number of core threads is 5, the maximum number of threads is 10, the queue size is 3, the survival time of threads exceeding the number of core threads is 5 seconds, and the
*
* @ throws Exception
, /
Private void threadPoolExecutorTest7 () throws Exception {
ThreadPoolExecutor threadPoolExecutor = testShutdownCommon ()
/ / terminate the thread pool after 1 second
Thread.sleep (1000L)
ThreadPoolExecutor.shutdown ()
/ / failed to submit prompt again
ThreadPoolExecutor.submit (
New Runnable () {
@ Override
Public void run () {
System.out.println ("add a task")
}
});
/ / result analysis
/ / 1, 10 tasks were executed, 3 tasks entered the queue to wait, and 2 tasks were rejected
/ / 2. After calling shutdown, do not receive new tasks and wait for the completion of task 13.
/ / 3. After the additional task thread pool is closed, it can no longer be submitted and will be refused to execute.
}
/ * *
* 8. Terminate threads immediately: thread pool information: number of core threads 5, maximum number 10, queue size 3, thread survival time beyond the number of core threads: 5 seconds, specify the rejection policy of
*
* @ throws Exception
, /
Private void threadPoolExecutorTest8 () throws Exception {
/ / create a thread pool with a maximum of 10 core threads and a maximum waiting queue of 3, that is, a maximum of 13 tasks.
/ / the default policy is to throw a RejectedExecutionException exception, java.util.concurrent.ThreadPoolExecutor.AbortPolicy
ThreadPoolExecutor threadPoolExecutor = testShutdownCommon ()
/ / terminate the thread pool after 1 second
Thread.sleep (1000L)
List shutdownNow = threadPoolExecutor.shutdownNow ()
/ / failed to submit prompt again
ThreadPoolExecutor.submit (
New Runnable () {
@ Override
Public void run () {
System.out.println ("add a task")
}
});
System.out.println ("unfinished tasks are:" + shutdownNow.size ())
/ / result analysis
/ / 1, 10 tasks were executed, 3 tasks entered the queue to wait, and 2 tasks were rejected
/ / 2. After calling shutdownnow, 3 threads in the queue no longer execute and 10 threads are terminated
/ / 3. After the additional task thread pool is closed, it can no longer be submitted and will be refused to execute.
}
Public static void main (String [] args) throws Exception {
Demo7 demo7 = new Demo7 ()
/ / demo7.threadPoolExecutorTest1 ()
/ / demo7.threadPoolExecutorTest2 ()
/ / demo7.threadPoolExecutorTest3 ()
/ / demo7.threadPoolExecutorTest4 ()
/ / demo7.threadPoolExecutorTest5 ()
/ / demo7.threadPoolExecutorTest6 ()
/ / demo7.threadPoolExecutorTest7 ()
Demo7.threadPoolExecutorTest8 ()
}
} about the thread pool implementation principle is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.