ScheduledExecutorService可以创建一个执行任务的线程池,并在指定的时间间隔内运行任务,或在指定的时间执行一次任务。它可以用于编写定时任务、周期性任务和延迟任务等场景。例如,可以使用ScheduledExecutorService来定时备份数据库,定期清理临时文件,或者在指定时间向用户发送提醒消息。
在下面的例子中,监控你要执行的代码部分,如果这段代码执行的时间超过了你的定义,你可以终止它继续后面的代码。
在执行future.cancel(true)后,如果任务还没有执行完毕,那么Future的状态会被设置为CANCELLED。此时,如果你希望在Callable的while中控制任务的停止,你可以在while循环中判断Thread.currentThread().isInterrupted(),如果返回true,则代表任务已经被取消,可以退出while循环,终止任务的执行。
调用shutdownNow()方法后,如果还有其他的任务在执行,那么它们也将被取消,并且shutdownNow()方法将会尝试停止正在执行的线程。因此,你需要注意处理可能的线程中断异常。
注意:经过我的测试,即使使用executorService.shutdownNow(); // 关闭线程池,task中的任务如果已经开始了,我的代码卡在一个接口调用部分(要调用几十秒),那么task还是会继续执行完毕。
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
try {
Callable<Object> task = () -> {
long startTime = System.currentTimeMillis();
// ...要进行的操作
Object obj = null;
//myDoingEnd...
long endTime = System.currentTimeMillis();
System.out.println("执行时间:" + (endTime - startTime));
return obj;
};
Future<Object> future = executorService.schedule(task, 0, TimeUnit.SECONDS);
long futureStartTime = System.currentTimeMillis();
while (!future.isDone()) {
Thread.sleep(1000);
System.out.println("等待时间:" + (System.currentTimeMillis() - futureStartTime) + "ms");
if (System.currentTimeMillis() - futureStartTime > 10000) {
future.cancel(true);
System.out.println("===操作时间大于xx,已强行终止");
return null;
}
}
return future.get(1, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
return null;
}finally {
executorService.shutdown(); // 关闭线程池
// executorService.shutdownNow();
}
}