通过 @PreDestroy 和 bean 中配置 destroy-method 实现该功能
java 代码中:
class TestClass {
private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
public TestClass() {
4: scheduledExecutorService.scheduleAtFixedRate(task1, 1, 5, TimeUnit.MINUTES);
5: }
6:
new Runnable() {
void run() {
);
10: }
11: }
12:
13: @PreDestroy
void DestroyMethod() {
15: scheduledExecutorService.shutdownNow();
16: }
17: }
bean 配置文件中:
5: http://www.springframework.org/schema/beans
6: http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7: http://www.springframework.org/schema/tx
8: http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
9: http://www.springframework.org/schema/aop
10: http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
11: http://www.springframework.org/schema/task
>
13:
>
>
>