spring整合執行緒池
定義兩個類:
/*** 執行緒池配置類
* @author Administrator
*
*/
public class ThreadPoolConfig {
//池中所儲存的執行緒數,包括空閒執行緒。
private int corePoolSize;
//池中允許的最大執行緒數。
private int maximumPoolSize;
//當執行緒數大於核心時,此為終止前多餘的空閒執行緒等待新任務的最長時間。
private long keepAliveTime;
//時間單位名。
private String timeUnitName;
//任務佇列的長度
private int queueSize;
//執行前用於保持任務的佇列。此佇列僅由保持 execute 方法提交的 Runnable 任務。
private BlockingQueue<Runnable> workQueue;
//用於設定建立執行緒的工廠
ThreadFactory threadFactory;
//由於超出執行緒範圍和佇列容量而使執行被阻塞時所使用的處理程式。
private RejectedExecutionHandler rejectedExecutionHandler;
public ThreadFactory getThreadFactory() {
if (threadFactory == null) {
synchronized (this) {
if (threadFactory == null) {
threadFactory = new ThreadFactory() {
public Thread newThread(Runnable r) {
return new Thread(r);
};
};
}
}
}
return threadFactory;
}
public void setThreadFactory(ThreadFactory threadFactory) {
this.threadFactory = threadFactory;
}
public int getCorePoolSize() {
return corePoolSize;
}
public void setCorePoolSize(int corePoolSize) {
this.corePoolSize = corePoolSize;
}
public int getMaximumPoolSize() {
return maximumPoolSize;
}
public void setMaximumPoolSize(int maximumPoolSize) {
this.maximumPoolSize = maximumPoolSize;
}
public long getKeepAliveTime() {
return keepAliveTime;
}
public void setKeepAliveTime(long keepAliveTime) {
this.keepAliveTime = keepAliveTime;
}
public TimeUnit getUnit() {
for (TimeUnit unit : TimeUnit.values()){
if (unit.name().equalsIgnoreCase(timeUnitName))
return unit;
}
return null;
}
public BlockingQueue<Runnable> getWorkQueue() {
return workQueue;
}
public void setWorkQueue(BlockingQueue<Runnable> workQueue) {
this.workQueue = workQueue;
}
public RejectedExecutionHandler getRejectedExecutionHandler() {
return rejectedExecutionHandler;
}
public void setRejectedExecutionHandler(
RejectedExecutionHandler rejectedExecutionHandler) {
this.rejectedExecutionHandler = rejectedExecutionHandler;
}
public int getQueueSize() {
return queueSize;
}
public void setQueueSize(int queueSize) {
this.queueSize = queueSize;
}
public String getTimeUnitName() {
return timeUnitName;
}
public void setTimeUnitName(String timeUnitName) {
this.timeUnitName = timeUnitName;
}
}
/**
* 執行緒池工廠類,getObject() 方法用來建立執行緒池物件
* @author Administrator
*
*/
public class ThreadPoolFactory implements FactoryBean<ThreadPoolExecutor>{
private ThreadPoolConfig config;
@Override
public ThreadPoolExecutor getObject() throws Exception {
ThreadPoolExecutor pool = null;
int corePoolSize = config.getCorePoolSize();
int maximumPoolSize = config.getMaximumPoolSize();
long keepAliveTime = config.getKeepAliveTime();
int queueSize = config.getQueueSize();
TimeUnit unit = config.getUnit();
ThreadFactory threadFactory = config.getThreadFactory();
RejectedExecutionHandler handler = config.getRejectedExecutionHandler();
BlockingQueue<Runnable> queue = config.getWorkQueue();
if(queue == null)
queue = new ArrayBlockingQueue<Runnable>(queueSize);
if(handler == null){
if (threadFactory == null)
pool = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize, keepAliveTime, unit, queue);
else
pool = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize, keepAliveTime, unit, queue, threadFactory);
}else{
if (threadFactory == null)
pool = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize, keepAliveTime, unit, queue, handler);
else
pool = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize,keepAliveTime, unit, queue, threadFactory, handler);
}
return pool;
}
@Override
public Class<?> getObjectType() {
return ThreadPoolExecutor.class;
}
@Override
public boolean isSingleton() {
return true;
}
public ThreadPoolConfig getConfig() {
return config;
}
public void setConfig(ThreadPoolConfig config) {
this.config = config;
}
}
下面是整合配置到spring:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 任務佇列配置-->
<bean id="workQueue" class="java.util.concurrent.ArrayBlockingQueue" >
<constructor-arg type="int" value="1000000" />
</bean>
<bean id="rejectedExecutionHandler" class="java.util.concurrent.ThreadPoolExecutor.AbortPolicy" />
<!-- 執行緒池基本配置-->
<bean id="threadPoolConfig" class="com.cloudsea.sys.compenent.threadpool.ThreadPoolConfig">
<property name="corePoolSize" value="100"/>
<property name="maximumPoolSize" value="2000"/>
<property name="keepAliveTime" value="5"/>
<property name="timeUnitName" value="MINUTES"/>
<property name="workQueue">
<ref bean="workQueue" />
</property>
<property name="rejectedExecutionHandler">
<ref bean="rejectedExecutionHandler" />
</property>
</bean>
<!-- 執行緒池建立工廠-->
<bean id="threadPoolFactory" class="com.cloudsea.sys.compenent.threadpool.ThreadPoolFactory">
<property name="config">
<ref bean="threadPoolConfig" />
</property>
</bean>
</beans>
下面是在編寫的測試程式碼,直接在web系統中執行執行緒池,這裡我就直接寫在登陸程式碼中了
@Controller
public class LoginController {
@Autowired
ThreadPoolFactory threadPoolFactory;
@Resource(name = "writeFileProcess")
Process writeFileProcess;
@AccessRequired
@RequestMapping(value="/index", method=RequestMethod.GET)
public String index(User user) throws Exception{
ThreadPoolExecutor pool = threadPoolFactory.getObject();
final File file = new File("D:/1.txt");
if (file.exists())
file.delete();
file.createNewFile();
PrintWriter writer = new PrintWriter(new FileOutputStream(file), true);
for (int i = 0; i < 100000; i++){
Runnable runnable = new PrintThread(i, writer);
pool.execute(runnable);
}
writeFileProcess.write("OfficeStatus");
return "sys/user/login/login";
}
}
這個類定義執行緒執行任務,這裡就是簡單的把傳過來的資料寫到流中
class PrintThread implements Runnable{
int val;
PrintWriter writer;
public PrintThread(int val, PrintWriter writer){
this.val = val;
this.writer = writer;
}
public void run() {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
writer.println(val);
}
}
配置好後,啟動web系統,登陸系統首頁時,然後執行緒池就會啟動,把1到100000的資料,用多執行緒併發的寫到D:/1.txt中