java 定时器 的中断程序,Java可自定义中断定时器的实现

本文介绍了如何使用ScheduledExecutorService来创建可中断的定时任务,区别于Timer的使用,ScheduledExecutorService提供了更灵活的控制方式。在任务执行过程中,如果满足特定条件,可以通过调用Future.cancel()来中断任务。文章详细阐述了任务的创建、中断逻辑,并展示了如何维护和管理任务的Future实例。
摘要由CSDN通过智能技术生成

Java定时器的实现一般使用的是Timer和ScheduledExecutorService

使用Timer的时候需要自定义一个task继承TimerTask,在task中封装业务逻辑

然后再使用Timer.scheduleAtFixedRate 按照固定速率执行task中的逻辑

本文介绍的是ScheduledExecutorService来实现可中断定时器

我们首先看一下scheduleAtFixedRate 的javaDoc

If any execution of the task encounters an exception, subsequent executions are suppressed.Otherwise, the task will only terminate via cancellation or termination of the executor.

通过javaDoc可以发现需要终止task的方法有三种:

执行shutdown(),但是这个会终止整个Executor

task抛出异常,在task执行到需要中断的时候,抛出异常

取消当前执行的task

执行shutdown()肯定是不合适的,通过主动抛异常的方式来中断也不好

所以使用取消当前task的方式来实现自定义中断

首先看一下scheduleAtFixedRate的源码,我们注意到这个方法返回的是ScheduledFuture>

public ScheduledFuture> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);

ScheduledFuture是继承了Future的

public interface ScheduledFutureextends Delayed, Future

这样就可以通过Future.cancel()的方式来取消当前task实现自定义中断

初始化ScheduledExecutorService 线程池

private ScheduledExecutorService service = Executors.newScheduledThreadPool(20,new NamedThreadFactory("Schedule-Task"));

实现业务逻辑和退出条件

实现思路是通过保存每个task的Future对象,在满足条件是,执行future.cancel()

// 当前正在执行的task合集

private Mapfutures = new HashMap<>();

// 执行定时任务的时间间隔

private int period = 5;

private String taskPrefix ="DoSomething-Task";

public void executeTask(String id) {

// 已有当前定时任务在执行时,删掉当前任务,然后重新启动

if (futures.containsKey(taskPrefix+id)) {

futures.get(taskPrefix+id).cancel(true);

futures.remove(taskPrefix+id);

}

Future future = service.scheduleAtFixedRate(new Runnable() {

@Override

public void run() {

int currentNum = cacheService.getCurrentShowNum(id);

// 满足中断task条件

if ( /* 条件逻辑 */) {

Future currentFuture = futures.get(taskPrefix+id);

currentFuture.cancel(true);

// 任务执行完成

futures.remove(taskPrefix+id);

} else {

// 执行task业务逻辑

}

}

},0,period, TimeUnit.MINUTES);

futures.put(taskPrefix+id,future);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值