The recent notiy system has finally come to an end. It has entered the QA testing stage. It is really memorable for the days when the real code was actually used! It has not been such a smooth feeling for a long time. It doesn't mean that there is no chance of coding at ordinary times, mainly because more business logic is involved at ordinary times;
In ipvy systemLightweight and low couplingThe core of concurrent operations is mainly based on the concurrent package of JDK, donated by Doug Lea. Below we will review several important classes in this package, as well as important methods and attributes;
Several important interfaces in concurrent and package sending include:Executor, executorservice, scheduledexecutorservice;
Important implementation classes include:Scheduledthreadpoolexecutor, threadpoolexecutor;
For the class diagrams of these interfaces and implementation classes, see the UML diagram at the end of the document. The diagram marks some important attributes and methods in red, which can be highlighted;
Let's talk about it first.Java. util. Concurrent. threadpoolexecutor, That is, the thread pool we often talk about. Through this class, the application can be used directly, as long as different parameters are set during initialization. The main parameters include the following:
- Corepoolsize: Minimum number of thread pool maintenance threads
- Maximumpoolsize: Maximum number of thread pool maintenance threads
- KeepAliveTime: The idle time allowed by the thread pool maintenance thread
- Unit: The unit of idle time allowed by the thread pool maintenance thread
- Workqueue: Buffer Queue used by the thread pool
- Handler: Processing policy of the thread pool to reject tasks
A task is added to the thread pool using the execute (runnable) method. A task is a runnable object. The execution method of a task is the run () method of a runnable object. Note:Runnable, not thread.
When a task is added to the thread pool through the execute (runnable) method:
- If the number of threads in the thread pool is smaller than corepoolsize, a new thread should be created to process the added tasks even if all threads in the thread pool are idle.
- If the number in the thread pool is equal to corepoolsize, but the Buffer Queue workqueue is not full, the task is put into the buffer queue.
- If the number in the thread pool is greater than corepoolsize, the Buffer Queue workqueue is full, and the number in the thread pool is smaller than maximumpoolsize, a new thread is created to process the added task.
- If the number in the thread pool is greater than corepoolsize, the Buffer Queue workqueue is full, and the number in the thread pool is equal to maximumpoolsize, the handler policy is used to process the task.
- From the above we can see that the priority of processing tasks in the thread pool is:
Core Thread corepoolsize, task queue workqueue, and maximumpoolsize. If all three are full, handler is used to process the rejected task.
When the number of threads in the thread pool is greater than corepoolsize, if the idle time of a thread exceeds KeepAliveTime, the thread will be terminated. In this way, the thread pool can dynamically adjust the number of threads in the pool.
The optional parameters of unit are several static attributes in Java. util. Concurrent. timeunit:
Nanoseconds, microseconds, milliseconds, and seconds.
Handler has four options by default, and can be expanded by itself, but be careful:
- Threadpoolexecutor. abortpolicy: Java. util. Concurrent. rejectedexecutionexception is thrown directly;
- Threadpoolexecutor. callerrunspolicy: the main thread directly tries to execute the task. When the task is added to the thread pool, the task is added to the thread pool. This operation is executed repeatedly, it can effectively reduce the speed at which the main thread adds tasks to the thread pool;
- Threadpoolexecutor. discardoldestpolicy: directly discard the old task, that is, discard the thread that first joins the queue in the thread pool;
- Threadpoolexecutor. discardpolicy: directly discard the current task;
Let's talk about it again.Java. util. Concurrent. scheduledthreadpoolexecutorThis class is a subclass of threadpoolexecutor, so all of the features described above are available. In addition, he also has some of his own unique attributes and methods:
- Schedule (runnable command, long delay, timeunit Unit)
This method creates and executes a one-time task enabled after a given delay;
- Scheduleatfixedrate (runnable command, long initialdelay, long period, timeunit Unit)
This method creates and executes a scheduled task that is enabled for the first time after a given initial delay. Subsequent tasks have a specified cycle, that is, they will be executed after initialdelay, run the command after initialdelay + period, and then after initialdelay + 2 * period.
- Schedulewithfixeddelay (runnable command, long initialdelay, long delay, timeunit Unit)
This method creates and executes a scheduled task that is enabled for the first time after a given initial delay. Subsequently, there is a given delay between each execution termination and the next execution start.
The concurrent package is also encapsulated in spring.FixedrateOrFixeddelayIt is very convenient to schedule tasks. Compared with the quartz implementation, the configuration file is much less, if you are interested, refer to Chapter 1 scheduled scheduling and thread pool in spring in the spring document, focusing on two types:
Org. springframework. Scheduling. Concurrent. scheduledexecutorfactorybean
Org. springframework. Scheduling. Concurrent. scheduledexecutortask
Org. springframework. Scheduling. Concurrent. threadpooltaskexecutor
Appendix: UML diagrams of several core interfaces and classes in the concurrent package: