Detailed explanation and example code of java thread pool executorservice

@H_ 404_ 1@Java Thread pool executorservice

@H_ 404_ 1@1. Thread pool

1.1 under what circumstances are thread pools used

1.2 benefits of using thread pool

@H_ 404_ 1@2.ExecutorService And executors

2.1 introduction

Executorservice is an interface that inherits executor,

The executor is also an interface that contains only one method:

The top-level interface of thread pool in Java is excator, but strictly speaking > > executor is not a thread pool, but a tool for executing threads. The real thread > pool interface is executorservice

@H_ 404_ 1@3.Executors

It is a static factory class, which can produce different types of thread pools. Some source codes are as follows:

First look at a specific example to illustrate the similarities and differences between them

4. Four common thread pools

4.1 CachedThreadPool

Cachedthreadpool will create a cache to cache the initialized threads, terminate and remove the threads that have not been used for 6 seconds from the cache If a thread is available, use the previously created thread If no thread is available, a new thread is created

@H_ 404_ 1@. Reuse:

For a cache pool, first check whether there are previously created threads in the pool. If there are, reuse. If not, create a new thread to join the pool,

@H_ 404_ 1 @ usage scenario:

Cache pool is usually used to perform asynchronous tasks with short lifetime, so it is not used much in some connection oriented daemon servers

@H_ 404_ 1 @ timeout:

Threads that can reuse must be threads in the pool within timeout idle. The default timeout is 60s. If the duration exceeds this idle, the thread instance will be terminated and the pool will be removed

@H_ 404_ 1 @ end:

The thread put into the cachedthreadpool does not have to worry about its end. If it is inactive beyond timeout, it will be automatically terminated

@H_ 404_ 1 @ example explanation:

Remove the note of (2) and run the. The running results are as follows:

It can be seen from the results that the four tasks are executed alternately

@H_ 404_ 1@4.2FixedThreadPool

In the fixedthreadpool, there is a fixed size pool,

If the current tasks to be executed exceed the pool size, the extra tasks will be in a waiting state until there are idle threads to execute the tasks. If the current task to be executed is smaller than the pool size, the idle thread will not be destroyed

@H_ 404_ 1 @ reuse:

Fixedthreadpool is similar to cachethreadpool and can be reused, but new threads cannot be created at any time

@H_ 404_ 1 @ fixed number

Its uniqueness is that at any point in time, at most a fixed number of active threads can exist. At this time, if a new thread needs to be established, it can only be placed in another queue and wait until a thread in the current thread terminates and is directly removed from the pool

@H_ 404_ 1 @ timeout:

Unlike cachethreadpool, fixedthreadpool does not have an idle mechanism

@H_ 404_ 1 @ usage scenario:

Therefore, fixedthreadpool is mostly used for some very stable and fixed regular concurrent threads, and is mostly used for servers

@H_ 404_ 1 @ source code analysis:

From the source code of the method, the cache pool and fixed pool call the same underlying pool, but the parameters are different The number of fixed pool threads is fixed, and the number of cache pool threads is 0 second idle (no idle). 0-integer.max_value is supported (obviously, the resource bearing capacity of the host is not considered at all), and 60 second idle is supported

@H_ 404_ 1 @ example explanation:

Remove the note of (1), and the operation results are as follows:

A thread pool with a fixed size of 3 is created, and then four tasks are executed circularly. From the output results, it can be seen that the first three tasks are executed first, and then the idle threads execute the fourth task

4.3SingleThreadExecutor

Remove the (3) note The execution results are as follows:

@H_ 404_ 1@ the four tasks are performed sequentially

4.4 ScheduledThreadPool

Scheduledthreadpool is a fixed size thread pool, similar to fixedthreadpool, which executes scheduled tasks The result obtained by removing the comment in (4) is the same as that obtained by fixedthreadpool. The main of scheduledthreadpool is not here, but a scheduled task. See the following example:

@H_ 404_ 1 @ result:

After the program runs for 2 seconds, the result will be displayed, indicating that the thread executes after 2 seconds

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>