API:
| Method Summary | ||
|---|---|---|
void | afterPropertiesSet()Calls initialize() after the container applied all property values. | |
protected edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue | createQueue(int queueCapacity)Create the BlockingQueue to use for the ThreadPoolExecutor. | |
void | destroy()Calls shutdown when the BeanFactory destroys the task executor instance. | |
void | execute(Runnable task)Implementation of both the JSR-166 backport Executor interface and the Spring TaskExecutor interface, delegating to the ThreadPoolExecutor instance. | |
void | execute(Runnable task, long startTimeout)Execute the given task. | |
int | getActiveCount()Return the number of currently active threads. | |
int | getCorePoolSize()Return the ThreadPoolExecutor's core pool size. | |
int | getKeepAliveSeconds()Return the ThreadPoolExecutor's keep-alive seconds. | |
int | getMaxPoolSize()Return the ThreadPoolExecutor's maximum pool size. | |
int | getPoolSize()Return the current pool size. | |
edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor | getThreadPoolExecutor()Return the underlying ThreadPoolExecutor for native access. | |
void | initialize()Creates the BlockingQueue and the ThreadPoolExecutor. | |
boolean | prefersShortLivedTasks()This task executor prefers short-lived work units. | |
void | setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)Specify whether to allow core threads to time out. | |
void | setBeanName(String name)Set the name of the bean in the bean factory that created this bean. | |
void | setCorePoolSize(int corePoolSize)Set the ThreadPoolExecutor's core pool size. | |
void | setKeepAliveSeconds(int keepAliveSeconds)Set the ThreadPoolExecutor's keep-alive seconds. | |
void | setMaxPoolSize(int maxPoolSize)Set the ThreadPoolExecutor's maximum pool size. | |
void | setQueueCapacity(int queueCapacity)Set the capacity for the ThreadPoolExecutor's BlockingQueue. | |
void | setRejectedExecutionHandler(edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)Set the RejectedExecutionHandler to use for the ThreadPoolExecutor. | |
void | setThreadFactory(edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory threadFactory)Set the ThreadFactory to use for the ThreadPoolExecutor's thread pool. | |
void | setThreadNamePrefix(String threadNamePrefix)Specify the prefix to use for the names of newly created threads. | |
void | setWaitForTasksToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown)Set whether to wait for scheduled tasks to complete on shutdown. | |
void | shutdown()Perform a shutdown on the ThreadPoolExecutor. | |
| submit(Callable<T> task)Submit a Callable task for execution, receiving a Future representing that task. | |
Future<?> | submit(Runnable task)Submit a Runnable task for execution, receiving a Future representing that task. | |
executor.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<!-- 如上添加 相关task标签 /|\ -->
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.10.RELEASE</version>
</dependency> -->
<!-- spring线程池 -->
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 线程池维护线程的最少数量 -->
<property name="corePoolSize" value="5" />
<!-- 线程池维护线程所允许的空闲时间,默认为60s -->
<property name="keepAliveSeconds" value="200" />
<!-- 线程池维护线程的最大数量 -->
<property name="maxPoolSize" value="20" />
<!-- 缓存队列最大长度 -->
<property name="queueCapacity" value="20" />
<!-- 对拒绝task的处理策略 线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy;默认为后者 -->
<property name="rejectedExecutionHandler">
<!-- AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 -->
<!-- CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,可以有效降低向线程池内添加任务的速度 -->
<!-- DiscardOldestPolicy:抛弃旧的任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
<!-- DiscardPolicy:抛弃当前任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
<property name="waitForTasksToCompleteOnShutdown" value="true" />
</bean>
</beans>
测试类ExecutorTest1.java的代码:
package com.soecode.lyf.web.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
public class ExecutorTest1 {
@Test
public void test() {
try {
ApplicationContext context1 = new ClassPathXmlApplicationContext("executor.xml");
ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor)context1.getBean("taskExecutor");
for(int i=0;i<10;i++){
taskExecutor.execute(new Runnable(){
@Override
public void run() {
System.out.println(Thread.currentThread());
}
});
}
System.out.println("main方法结束");
} catch (Exception e) {
e.printStackTrace();
}
}
}
打印的日志:
[org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@55816088: startup date [Thu Feb 08 15:16:54 CST 2018]; root of context hierarchy
[org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [executor.xml]
[org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] - Initializing ExecutorService 'taskExecutor'
Thread[taskExecutor-1,5,main]
Thread[taskExecutor-3,5,main]
Thread[taskExecutor-4,5,main]
Thread[taskExecutor-2,5,main]
main方法结束
Thread[taskExecutor-1,5,main]
Thread[taskExecutor-1,5,main]
Thread[taskExecutor-1,5,main]
Thread[taskExecutor-1,5,main]
Thread[taskExecutor-2,5,main]
Thread[taskExecutor-5,5,main]
本文介绍了如何在Spring框架中配置和使用多线程。通过查看executor.xml的配置,我们可以了解到具体的线程池设置,这有助于提高应用程序的并发性能。在实际运行中,这些配置将影响到日志打印的输出和线程调度策略。
910

被折叠的 条评论
为什么被折叠?



