Today’s sharing started, please give us more advice ~

1. Executors

Executors class belongs to java.util. Concurrent package;

Create a thread pool by following two methods: ThreadPoolExecutor and Executors;

Exector (Static Executor Factory) creates a thread pool.

Factory and tool methods Executor, ExecutorService, ScheduledExecutorService, ThreadFactory, and Callable classes defined in this package;

The jdk1.8API explains as follows:

2. Executors Structure

3. Common methods for Executors

Public static ExecutorService newFixedThreadPool(int nThreads) A thread pool with a fixed number of threads. When threads are idle, they are not recycled unless the pool is closed. When all threads are active, new tasks wait until a thread is free.

Public static ExecutorService newSingleThreadExecutor() creates a single thread. It applies to tasks that need to be executed sequentially; And no more than one thread is active at any point in time. If a thread dies, a thread is restarted to continue executing the task.

Public static ExecutorService newCachedThreadPool() creates a thread pool that creates new threads as needed, but recycles previously constructed threads as they become available, or creates a new thread and adds it to the pool if there are no available threads. Threads that are not used for 60 seconds are terminated and removed from the cache; Threads are created when they are used and destroyed when they are not used.

  • Public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) Creates a thread pool that can schedule commands to run after a given delay or to execute them periodically. Supports the execution of scheduled or periodic tasks.

Create a thread pool that maintains enough threads to support a given level of parallelism. And you can use multiple queues to reduce contention. (New method in JDK1.8)

4. Create a thread pool in two ways (use Executors to create a thread pool).

1. Example newFixedThreadPool method

code

The following figure shows the output

Conclusion: In the example, a fixed number of 3 threads are created. As shown in the screenshot of the output result, the number of iterations is 5. After a round of execution (3 times), the execution is paused for one second until a thread is free, and the fourth execution is continued.

2. Example newSingleThreadExecutor method

code

The following figure shows the output

Conclusion: The example creates a single thread that sleeps for one second after each task to ensure that the tasks are executed sequentially.

The newCachedThreadPool method

code

The following figure shows the output

Conclusion: In the example, thread pools with cached threads are created as needed and previously constructed threads are reused when available.

4. Example newScheduledThreadPool method

code

The following figure shows the output

Conclusion: In the example, a thread pool is created to execute periodic or scheduled tasks. Based on the output, the task takes effect one minute later.

Executors Create a thread pool

The ThreadPoolExecutor constructor is called whenever a thread pool of any type is created (newFixedThreadPool, newSingleThreadExecutor, newCachedThreadPool, and so on).

Parsing arguments in the ThreadPoolExecutor constructor

CorePoolSize Specifies the maximum number of core threads in a thread pool.

MaximumPoolSize Specifies the maximum number of threads (both core and non-core) running in the thread pool.

KeepAliveTime The maximum length of time an idle thread (non-core thread only) can live in the thread pool;

Unit Unit of keepAliveTime. Used with keepAliveTime.

WorkQueue Stores the blocking queue of tasks.

Handler Thread pool saturation policy.

Understanding the Executor interface

Executor belongs to the java.util.concurrent package;

Executor is the core interface of the task execution mechanism;

Executor interface class diagram structure

According to the structure of the class diagram,

ThreadPoolExecutor inherits the AbstractExecutorService interface;

The AbstractExecutorService interface implements the ExecutorService interface.

ExecutorService inherits the Executor interface;

So the following sections focus on the ThreadPoolExecutor class.

Common Executor interface methods

Void execute(Runnable command) Executes a given command at a future time. This command can be executed in a new thread, a merged thread, or in the calling thread, implemented by Executor.

There are two ways to create a thread pool (ThreadPoolExecutor).

Constructor in ThreadPoolExecutor class

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue,defaultHandler)

ThreadPoolExecutor constructor parameter parsing

CorePoolSize Specifies the maximum number of core threads in a thread pool.

MaximumPoolSize Specifies the maximum number of threads (both core and non-core) running in the thread pool.

KeepAliveTime The maximum length of time an idle thread (non-core thread only) can live in the thread pool;

Unit Unit of keepAliveTime. Used with keepAliveTime.

WorkQueue Stores the blocking queue of tasks.

Handler Thread pool saturation policy.

Example ThreadPoolExecutor class to create a thread pool

code

The following figure shows the output

Conclusion: The ThreadPoolExecutor constructor is called regardless of the type of thread pool you create (newFixedThreadPool, newSingleThreadExecutor, newCachedThreadPool, and so on).

Today’s share has ended, please forgive and give advice!