1、配置jdbc.properties添加: _spring
- - #------------ Task ------------ - task.core_pool_size=5 - task.max_pool_size=50 - task.queue_capacity=1000 - task.keep_alive_seconds=60_
2、配置Spring的applicationContext.xml添加:app
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!-- 核心線程數 --> <property name="corePoolSize" value="${task.core_pool_size}" /> <!-- 最大線程數 --> <property name="maxPoolSize" value="${task.max_pool_size}" /> <!-- 隊列最大長度 --> <property name="queueCapacity" value="${task.queue_capacity}" /> <!-- 線程池維護線程所容許的空閒時間,默認爲60s --> <property name="keepAliveSeconds" value="${task.keep_alive_seconds}" /> </bean> <!-- 註解式 --> <task:annotation-driven />
3、在實現層Impl聲明並注入:線程
@Resource(name = "taskExecutor") private TaskExecutor taskExecutor;
4、在方法裏調用:code
或者xml
** @Autowired private TaskExecutor taskExecutor; **
try { taskExecutor.execute(new Runnable() { public void run() { //這裏編寫處理業務代碼 } }); } catch (Exception e) { e.printStackTrace(); }