通过Future来取消正在执行的任务

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

public boolean timedExec(Callable c, long timeout, TimeUnit unit){
        Future future = task.submit(c);
        try {
            future.get(timeout,unit);
        }catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            System.out.println("====================task time out===============");
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            future.cancel(true);
        }
        return  false;
    }

转载于:https://my.oschina.net/tank1988/blog/745076

你可能感兴趣的:(通过Future来取消正在执行的任务)