spring提供了线程池的支持
查看基本
Java线程池
先是一个线程 ThreadTransCode.java
package com.enorth.lichen.transcode;
public class ThreadTransCode implements Runnable{
@Override
public void run() {
System.out.println( "转码开始.............." );
}
}
public class ThreadTransCode implements Runnable{
@Override
public void run() {
System.out.println( "转码开始.............." );
}
}
在spring配置文件中添加线程池的配置信息
<
bean
id
="taskExecutor"
class ="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
< property name ="corePoolSize" value ="3" />
< property name ="keepAliveSeconds" value ="200" />
< property name ="maxPoolSize" value ="5" />
< property name ="queueCapacity" value ="25" />
</ bean >
class ="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
< property name ="corePoolSize" value ="3" />
< property name ="keepAliveSeconds" value ="200" />
< property name ="maxPoolSize" value ="5" />
< property name ="queueCapacity" value ="25" />
</ bean >
在action中添加注入
<
bean
id
="saveVideoAction"
class ="com.enorth.lichen.action.video.SaveVideoAction"
scope ="prototype" >
< property name ="videoService" >
< ref bean ="videoService" />
</ property >
< property name ="groupService" >
< ref bean ="groupService" />
</ property >
< property name ="taskExecutor " >
< ref bean ="taskExecutor " />
</ property >
</ bean >
class ="com.enorth.lichen.action.video.SaveVideoAction"
scope ="prototype" >
< property name ="videoService" >
< ref bean ="videoService" />
</ property >
< property name ="groupService" >
< ref bean ="groupService" />
</ property >
< property name ="taskExecutor " >
< ref bean ="taskExecutor " />
</ property >
</ bean >
在action中调用
private TaskExecutor taskExecutor;
public TaskExecutor getTaskExecutor() {
return taskExecutor;
}
public void setTaskExecutor(TaskExecutor taskExecutor) {
this .taskExecutor = taskExecutor;
}
public String execute() throws Exception {
public TaskExecutor getTaskExecutor() {
return taskExecutor;
}
public void setTaskExecutor(TaskExecutor taskExecutor) {
this .taskExecutor = taskExecutor;
}
public String execute() throws Exception {
for ( int k = 0; k < 2; k++) {
taskExecutor.execute( new ThreadTransCode());
}
return SUCCESS;
}
以上就可以用spring实现java线程池的使用...
本文出自 “李晨专栏 ” 博客,请务必保留此出处http://lichen.blog.51cto.com/697816/162057