Spring thread pool Application

Source: Internet
Author: User

1. Understand TaskExecutor Interface
The TaskExecutor interface of Spring is equivalent to the java. util. concurrent. Executor interface. In fact, the main reason for its existence is to abstract the dependency on Java 5 when using the thread pool. This interface has only one method, execute (Runnable task), which accepts an execution task Based on the semantics and configuration of the thread pool. TaskExecutor was initially created to provide a thread pool abstraction for other Spring components as needed. For example, the ApplicationEventMulticaster component, the JMS AbstractMessageListenerContainer and Quartz integration both use TaskExecutor abstraction to provide thread pools. Of course, if your bean needs thread pool behavior, you can also use this abstraction layer.
 
2. Implementation class of TaskExecutor Interface
(1) SimpleAsyncTaskExecutor class
This implementation does not reuse any threads, or it starts a new thread every time it calls. However, it still supports setting a limit on the total number of concurrent threads. When the total number of concurrent threads is exceeded, new calls are blocked until some locations are released. If you need a real pool, continue.
 
(2) SyncTaskExecutor class
This implementation will not be executed asynchronously. On the contrary, each call is executed in the thread that initiates the call. It is mainly used when multithreading is not required, such as a simple test case.
 
(3) ConcurrentTaskExecutor class
This implementation is the packaging of Java 5 java. util. concurrent. Executor class. Another alternative is the ThreadPoolTaskExecutor class, which exposes the Executor configuration parameters as bean attributes. ConcurrentTaskExecutor is rarely needed, but ConcurrentTaskExecutor is another alternative if ThreadPoolTaskExecutor is not required.
(4) SimpleThreadPoolTaskExecutor class
This implementation is actually a subclass of the SimpleThreadPool class of Quartz, which listens to Spring's lifecycle callback. When you have a thread pool and need to combine Quartz and non-Quartz components, this is a typical use.
(5) ThreadPoolTaskExecutor class
It does not support replacement of java. util. concurrent packages or downloading. Doug Lea and Dawid Kurzyniec both adopt different package structures for java. util. concurrent implementation, which causes them to fail to run correctly. This implementation can only be used in the Java 5 environment, but it is the most commonly used in this environment. The exposed bean properties can be used to configure a java. util. concurrent. ThreadPoolExecutor and encapsulate it into a TaskExecutor. If you need more advanced classes, such as ScheduledThreadPoolExecutor, we recommend that you use ConcurrentTaskExecutor instead.
(6) TimerTaskExecutor class
This implementation uses a TimerTask as its implementation. Unlike SyncTaskExecutor, a method call is performed in an independent thread, although it is synchronized in that thread.
(7) WorkManagerTaskExecutor class
This implementation uses CommonJ WorkManager as its underlying implementation and is the most important class for configuring CommonJ WorkManager applications in Spring context. Similar to SimpleThreadPoolTaskExecutor, this class implements the WorkManager interface and can be directly used as WorkManager.
3. ThreadPoolTaskExecutor of the thread pool Demo
(1) Compile the test class
Java code
Import org. springframework. core. task. TaskExecutor;
 
Public class TaskExecutorExample {
Private TaskExecutor taskExecutor;
Public TaskExecutorExample (TaskExecutor taskExecutor ){
This. taskExecutor = taskExecutor;
}
Public void printMessages (){
For (int I = 0; I <25; I ++ ){
TaskExecutor.exe cute (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 );
}
}
}
 
(2) spring Configuration
Java code
<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>
<Bean id = "taskExecutorExample" class = "powercn. TaskExecutorExample">
<Constructor-arg ref = "taskExecutor"/>
</Bean>
 
(3) Call
Java code
ApplicationContext appContext = new ClassPathXmlApplicationContext ("applicationContext. xml ");

TaskExecutorExample te = (TaskExecutorExample) appContext. getBean ("taskExecutorExample ");
Te. printMessages ();
System. out. println ("11111111111111111111111 ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.