The thread pool constructor is shown below. We can customize some functions for project optimization

1. Pre-start the core thread

this.prestartAllCoreThreads();

How to use: Called in the constructor of a custom thread pool to reduce the performance penalty of thread creation when a real task is executed

2. Custom thread pool thread factory

Is the official provide the default thread pool factory Executors. DefaultThreadFactory (); We can follow suit by customizing the extension thread factory and adding some personalization.

As shown in the figure below, we can add a lot of functionality when we customize the thread pool thread factory.

1. Customize the thread name in the thread pool, so that we can quickly locate the thread online;

2. When creating a thread, set whether it is a daemon thread, etc

3. Detect and catch task exceptions

When a task is executed (execute method), the thread pool does not catch the exception thrown by the task. As a result, the task fails and the thread pool is destroyed.Thread Pool Principle】.

At work, due to the lack of information, problems can not be perceived and lead to online accidents, resulting in losses

By implementingUncaughtExceptionHandler interface of Thread, set up exception handling classes for thread pool threads to achieve task exception awareness and catch

4. Thread pool monitoring and alarms

A thread pool performs tasks that are a black box to us. Tasks may be executed directly; It may be temporarily stored in a queue and cannot be executed for a long time, which degrades system performance. Or tasks are discarded, and how many are discarded. Because of these uncertainties, it is impossible to adjust in a reasonable way when system performance is affected.

The following custom functions allow us to monitor it at a macro level

4.1 using polling to obtain state information in the thread pool (not recommended)

The thread pool provides many GET methods to obtain real-time information in the thread pool, but most of them are locking operations. Frequent use reduces the efficiency of thread pool processing tasks. So this approach is not recommended.

4.2. Customize extensions based on the hooks provided by the thread pool itself

When a thread executes each task through the runWorker method, it provides empty methods before and after each task for custom extension.

Methods before performing tasks:beforeExecute(wt, task);

Execute task: task.run();

Methods after task execution:afterExecute(task, thrown);

The following is an example of a custom extension:

beforeExecute(wt, task); Methods to expand

afterExecute(task, thrown); Methods to expand You can customize the beforeExecute and afterExecute methods to monitor task execution time, availability, and execution times

4.2.2 Monitoring rejected tasks

When a task is rejected, you can customize the CallerRunsPolicy rejection policy to monitor related information

The following is an example of custom extensions



You can customize the CallerRunsPolicy reject policy to monitor the execution time, availability, and execution times of rejected tasks

The above extensible approach can be abstracted out to interface programming

Handle multiple businesses through the chain of responsibility model

5. Thread pool parameters are dynamically modified

Dynamically modify thread pool parameters using ZooKeeper

5.1 thread pool provides set methods for information such as number of threads, idle time, etc

For details on how to modify a queue, see the previous article: Dynamically Changing the thread Pool queue size

6. Custom closing thread pools

Runtime.getruntime ().addShutdownHook is a thread that executes before the JVM is destroyed

7. Sense the end of the FutureTask task

When the task enters its final state (task completion, exception, cancellation, interruption), the unpark method is called to restore the thread suspended by calling the GET () method, and the done extension null method is called

When the futureTask task is finished, the done null method is called

When multiple tasks are executed, you can view the task execution sequence based on the done method

Usage Scenarios:

1. Batch task processing. The task whose DONE method is called first can be executed first. Speed up the processing of tasks

2. When submitting orders online, it is often necessary to consume multiple resources at the same time. But if one resource consumption fails, all resources are rolled back.

Therefore, the done method can be used to quickly perceive and process fast tasks. Anticipate failure and roll back. To improve efficiency

3. Some functionality of the done method is officially provided

Such as: ExecutorCompletionService tasks according to the complete order, stored in the queue. And that gets processed

8, summary

1, through reading the source code, can let us find a lot of extensible methods, for our custom function 2, view the specific implementation of the extensible method, you can find some official extension implementation, learn the use of it


——The End——

If it is useful to you, or you want to continue to pay attention to it, you can search for “Mailu Wuya” in the wechat public account.