Java实现任务超时处理方法
任务超时处理是比较常见的需求,比如在进行一些比较耗时的操作(如网络请求)或者在占用一些比较宝贵的资源(如数据库连接)时,我们通常需要给这些操作设置一个超时时间,当执行时长超过设置的阈值的时候,就终止操作并回收资源。Java中对超时任务的处理有两种方式:一种是基于异步任务结果的超时获取,一种则是使用延时任务来终止超时操作。下文将详细说明。
一、基于异步任务结果的超时获取
基于异步任务结果的获取通常是跟线程池一起使用的,我们向线程池提交任务时会返回一个Future对象,在调用Future的get方法时,可以设置一个超时时间,如果超过设置的时间任务还没结束,就抛出异常。接下来看代码:
public class FutureDemo {
static ExecutorService executorService= Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*2);
public static void main(String[] args) {
Future<String> future = executorService.submit(new Callable<String>() {
@Override
public String call() {
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
System.out.println("任务被中断。");
}
return "OK";
}
});
try {
String result = future.get(2, TimeUnit.SECONDS);
} catch (InterruptedException |ExecutionException | TimeoutException e) {
future.cancel(true);
System.out.println("任务超时。");
}finally {
System.out.println("清理资源。");
}
}}
运行代码,输出如下:

二、使用延时任务来终止超时操作
还有一种实现任务超时处理的思路是在提交任务之前先设置一个定时器,这个定时器会在设置的时间间隔之后去取消任务。当然如果任务在规定的时间内完成了,要记得取消定时器。首先来看一下我们的工作线程:
public class RunningTask {
private volatile boolean isStop;
public void stop(){
this.isStop=true;
}
public void doing() {
int i=1;
while (!isStop){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
}
}
System.out.println("任务被中断。");
}
}
这个工作线程每隔一秒钟会去检查下isStop变量,因此我们可以通过isStop变量来取消任务。至于取消任务的逻辑我们放在了定时器里面,代码如下:
public class CancelTask implements Runnable {
private RunningTask runningTask;
public CancelTask(RunningTask runningTask) {
this.runningTask = runningTask;
}
@Override
public void run() {
runningTask.stop();
}
}
可以看到,该定时器的作用就是在一定的时间之后去中断工作线程的运行。接下来测试一下:
public class ScheduleDemo {
static ScheduledExecutorService executorService= Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors()*2);
public static void main(String[] args) {
RunningTask runningTask=new RunningTask();
ScheduledFuture<?> scheduledFuture = executorService.schedule(new CancelTask(runningTask), 3, TimeUnit.SECONDS);
runningTask.doing();
if(!scheduledFuture.isDone()){
scheduledFuture.cancel(true);
}
}
}
运行结果如下:

可以看到,任务在超时之后也可以被取消。
总结
上一篇:Java进阶学习:jar打包详解
栏 目:JAVA代码
本文标题:Java实现任务超时处理方法
本文地址:http://www.codeinn.net/misctech/146924.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




