多线程之取消任务(中断线程)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sarafina527/article/details/89384133

Future接口

public interface Future<V> {

    /**
     * 尝试取消执行此任务。
     * mayInterruptIfRunning {@code true}如果执行此操作的线程任务应该中断; 否则,允许进行中任务去完成
     */
    boolean cancel(boolean mayInterruptIfRunning);


    boolean isCancelled();


    boolean isDone();

    /**
     * 阻塞等待结果
     */
    V get() throws InterruptedException, ExecutionException;

    /**
     * 阻塞等待结果,如果超过超时时间则抛出异常
     */
    V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}

sleep线程样例

    @Test
    public void FutureTest() throws Exception{
        Runnable sleepRun = new Runnable() {
            @Override
            public void run() {
                System.out.println("runnable start");
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("runnable end");

            }
        };
        Future<?> task = executor.submit(sleepRun);
        try {
            // 等待结果,如果超时则会抛出异常,finally里会取消任务,不在乎任务结果了
            System.out.println(task.get(1, TimeUnit.SECONDS));
        } catch (TimeoutException e) {
            //
        } catch (ExecutionException e) {
            // 重新抛出异常
        } finally {
            // 以上两种异常的情况任务会取消
            task.cancel(true); // 中断任务
        }
    }

执行结果 

由结果可见,在future调用cacle方式时,会发送中断信号给子线程,而子线程当时应处于sleep状态,会被中断唤醒。

死循环线程样例

@Test
    public void FutureCircleTest() throws Exception{
        Runnable circleRun = new Runnable() {
            @Override
            public void run() {
                System.out.println("runnable start");
                // 死循环的任务
                while (true) {
                    System.out.println("run circle ");
                }
            }
        };
        Future<?> task = executor.submit(circleRun);
        try {
            // 等待结果,如果超时则会抛出异常,finally里会取消任务,不在乎任务结果了
            System.out.println(task.get(1, TimeUnit.SECONDS));
        } catch (TimeoutException e) {
            //
        } catch (ExecutionException e) {
            // 重新抛出异常
        } finally {
            // 以上两种异常的情况任务会取消
            task.cancel(true); // 中断任务
        }
    }

执行结果

由结果可见,死循环线程超时后被终止了 

猜你喜欢

转载自blog.csdn.net/sarafina527/article/details/89384133