一 TaskExecutor interface
Spring’s TaskExecutor interface is equivalent to the Java.util.concurrent.Executor interface. In fact, the main reason for its existence is to abstract the dependence on Java 5 when using thread pools. This interface has only one method execute(Runnable task), which accepts an execution task according to the semantics and configuration of the thread pool.
TaskExecutor was originally created to provide a thread pool abstraction to other Spring components when needed. For example, the ApplicationEventMulticaster component, JMS's AbstractMessageListenerContainer, and the integration of Quartz all use the TaskExecutor abstraction to provide a thread pool. Of course, you can also use this abstraction layer if your beans require thread pool behavior.
二 TaskExecutor type
Some TaskExecutor implementations are predefined in the Spring distribution package. With them, you don't even need to implement it yourself anymore.
SimpleAsyncTaskExecutor Class
This implementation does not reuse any threads, or it starts a new thread every time it is called. However, it still supports setting a limit on the total number of concurrent threads. When the limit on the total number of concurrent threads is exceeded, new calls will be blocked until a position is released. If you need a real pool, read on.
SyncTaskExecutor Class
This implementation will not execute asynchronously. Instead, each call is executed in the thread that originated the call. Its main use is when multi-threading is not required, such as simple test cases.
ConcurrentTaskExecutor class
This implementation is a wrapper for the Java 5 java.util.concurrent.Executor class. There is another alternative, the ThreadPoolTaskExecutor class, which exposes the Executor's configuration parameters as bean properties. It is rarely necessary to use ConcurrentTaskExecutor, but if ThreadPoolTaskExecutor is not enough, ConcurrentTaskExecutor is another alternative.
SimpleThreadPoolTaskExecutor class
This implementation is actually a subclass of Quartz’s SimpleThreadPool class, which listens to Spring’s life cycle callbacks. This is its typical use when you have a thread pool that needs to be shared between Quartz and non-Quartz components.
ThreadPoolTaskExecutor class
It does not support any replacement or downport of the java.util.concurrent package. Both Doug Lea and Dawid Kurzyniec's implementations of java.util.concurrent use different package structures, causing them to not run correctly.
This implementation can only be used in the Java 5 environment, but it is the most commonly used in this environment. The bean properties it exposes can be used to configure a java.util.concurrent.ThreadPoolExecutor and wrap it into a TaskExecutor. If you need a more advanced class, such as ScheduledThreadPoolExecutor, we recommend that you use ConcurrentTaskExecutor instead.
TimerTaskExecutor class
This implementation uses a TimerTask as the implementation behind it. The difference between it and SyncTaskExecutor is that the method call is made in a separate thread, although it is synchronized in that thread.
WorkManagerTaskExecutor Class
CommonJ is a set of specifications jointly developed by BEA and IBM. These specifications are not Java ee standards, but they are common standards for BEA and IBM application server implementations
This implementation uses CommonJ WorkManager as its underlying implementation and is the most important to configure CommonJ WorkManager applications in Spring context the type. Similar to SimpleThreadPoolTaskExecutor, this class implements the WorkManager interface, so it can be used directly as a WorkManager.
Three Simple Examples of TaskExcutor
1 taskExcutor
package com.test;import org.springframework.core.task.TaskExecutor;public class MainExecutor { private TaskExecutor taskExecutor; public MainExecutor (TaskExecutor taskExecutor) { this.taskExecutor = taskExecutor; } public void printMessages() { for(int i = 0; i < 25; i++) { taskExecutor.execute(new MessagePrinterTask("Message" + i)); } } private class MessagePrinterTask implements Runnable { private String message; public MessagePrinterTask(String message) { this.message = message; } public void run() { System.out.println(message); } } }View Code
package com.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TaskTest {//本地测试,不用部署到tomcatpublic static void main(String[] args) { System.out.println("测试任务调度开始..."); ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml"); MainExecutor te = (MainExecutor)appContext.getBean("taskExecutorExample"); te.printMessages(); System.out.println("--------"); } }View Code
3.applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/spring-beans.dtd"><beans> <bean id="taskExecutorExample" class="com.test.MainExecutor"> <constructor-arg ref="taskExecutor" /> </bean> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="5" /> <property name="maxPoolSize" value="10" /> <property name="queueCapacity" value="25" /> </bean></beans>
The above is the detailed content of Introduction to TaskExecutor interface and types. For more information, please follow other related articles on the PHP Chinese website!