TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
一、配置jdbc.properties添加:- #------------ Task ------------" Y8 J* N1 O) F# V* I9 a8 b7 t
- task.core_pool_size=5- w* a( u8 R8 @7 W) R
- task.max_pool_size=509 J. `( i& D. x
- task.queue_capacity=10004 U5 @: v5 M* `0 N* u
- task.keep_alive_seconds=60
复制代码 # T* e8 j+ `- ]0 b1 y
二、配置spring的applicationContext.xml添加:% f) k- \0 K) K# u3 j
- <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">0 T a3 L( U$ Z; T* W
- <!-- 核心线程数 -->
( q2 ^6 f) i7 X& t: @9 b% ? - <property name="corePoolSize" value="${task.core_pool_size}" />2 v# s o6 g/ t3 a
- <!-- 最大线程数 -->
, _4 B k0 D: k" b H - <property name="maxPoolSize" value="${task.max_pool_size}" />; ^. e0 Q, K8 J8 A$ Q0 s! B# b2 k
- <!-- 队列最大长度 -->
+ P6 E; y/ _ h, j" _ - <property name="queueCapacity" value="${task.queue_capacity}" />
) @% I( `" m7 H; {, l4 L' } - <!-- 线程池维护线程所允许的空闲时间,默认为60s -->
- a/ z6 J& S0 }" C: a - <property name="keepAliveSeconds" value="${task.keep_alive_seconds}" />
: F2 Q+ R1 ]% L& E6 k1 i - </bean>, y7 c' s( o6 V
- <!-- 注解式 -->
2 U& z& O; v; S( _/ ? - <task:annotation-driven />
复制代码 三、在实现层Impl声明并注入:
8 ^9 z8 d. K7 B- n/ U& H( ?" S- @Resource(name = "taskExecutor")6 e5 [( u5 m4 ^
- private TaskExecutor taskExecutor;
复制代码
# m& _+ w# `- y$ Q' p0 Q四、在方法里调用:9 i- v2 \' Z9 M4 u) e* |, q
- try {1 _$ F7 r5 Q2 t0 n% W# c
- taskExecutor.execute(new Runnable() {' H1 c+ t2 F$ M# M4 ~, @) j: @5 o
- public void run() {
9 F6 s4 N+ H: x& T2 F: e - //这里编写处理业务代码' O, W) y1 q9 e- r) o6 w2 E& _+ [
- 1 R( E" O) \5 N/ N6 l2 ^8 n1 y5 @
- }4 M/ r1 M0 q, G2 e/ w' d
- });
9 D/ z4 i: x3 d3 h% }3 m/ f - } catch (Exception e) {4 Y" C \. M( X, Q
- e.printStackTrace();" @. X8 { L; x
- }
复制代码 8 h) H3 ~7 _$ L2 f
5 C2 @( Z% M S6 s/ t
|
|