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);
        }
    }
}

运行结果如下:

可以看到,任务在超时之后也可以被取消。

猜你喜欢

转载自www.cnblogs.com/hzjjames/p/java_timeout.html
今日推荐