在你运行方法中放@Async 注释并查看
@Async
public void run{
}
或者你可以
试试这个
<bean id="schedulerTask"
class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
<property name="mytaskClass" ref="mytaskClass" />
<property name="targetMethod" value="fooMethod" />
</bean>
<bean id="mytaskClass" class="foo.bar.Task" />
<bean id="timerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="schedulerTask" />
<property name="delay" value="10" />
<property name="period" value="5000" />
</bean>
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref local="timerTask" />
</list>
</property>
</bean>
然后你的课
package foo.bar;
public class Task{
public void fooMethod(){
// do task
}
}
根据要求添加
<!-- Thread pool related configurations -->
<bean name="workerThread" class="foo.WorkerThread"/>
<bean name="managerThread" class="foo.ManagerThread" >
<constructor-arg type="org.springframework.core.task.TaskExecutor" ref="taskExecutor" />
<constructor-arg type="foo.process.WorkerThread" ref="workerThread"/>
</bean>
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="30" />
<property name="queueCapacity" value="100" />
</bean>
<!-- End Thread pool related configurations -->
ManagerThread.java
public class ManagerThread {
private TaskExecutor taskExecutor=null;
private WorkerThread workerThread=null;
/**
* @param taskExecutor
* @param workerThread
*/
public ManagerThread(final TaskExecutor taskExecutor,final WorkerThread workerThread) {
this.taskExecutor = taskExecutor;
this.workerThread = workerThread;
}
/**
* Create a new thread and execte the requests
* @param parameter
*/
public synchronized void fire(final Object parameter) {
taskExecutor.execute( new Runnable() {
public void run() {
workerThread.execute( parameter );
}
});
}
WorkerThread.java
@Component
public class WorkerThread {
public void execute(final Object request) {
// do the job
}
}
您可以根据您的要求自定义它