Java 并发编程学习笔记(15) ----Future 和 Callable

本文深入解析了Future和Callable接口在Java并发编程中的应用,对比了Callable与Runnable的主要区别,包括Callable的call()方法能返回结果并声明抛出异常,而Runnable的run()方法则不能。此外,还详细介绍了如何使用cancel()和isCancelled()方法来控制线程的执行状态。

Future 和 Callable

1.1 Callable 和 Runnable 的区别

1.Callable 接口的call()方法可以有返回值,而Runnable接口的run()方法没有返回值。执行完Callable
接口中的任务后,返回值是通过接口进行获得的。
2.Callable 接口的call()方法可以声明抛出异常,而Runnale的接口run()方法不可以声明抛出异常。

1.2 方法cancel(boolean b) 和 isCancelled()的使用

方法cancel(boolean b)的参数 b 的作用是:如果线程正在运行则是否中断正在运行的线程,在代码中使用if(Thread.currentThread().isInterrupted())
进行配合。
方法cancel()的返回值代表发送取消任务的命令是否成功完成

1.3 代码


/**
 * 方法 cancle(boolean mayInterruptIfRunning) 的参数 mayInterruptIfRunning的作用是:
 *      如果线程正在运行则是否中断正在运行的线程,
 *      在代码中使用if(Thread.currentThread().isInterrupted())进行配合。
 * 方法 cancel()的返回值代表发送取消任务的命令是否成功完成
 */
package com.lhc.concurrent.future;

import java.util.concurrent.*;

public class MyCallable implements Callable{
    @Override
    public String call() throws Exception {
        boolean bool = true;
        while(bool){
            if(Thread.currentThread().isInterrupted()){
                throw new InterruptedException();
            }
            System.out.println("运行中");
        }
        return "hello world";
    }

    public static void main(String[] args) throws Exception{
        MyCallable myCallable = new MyCallable();

        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, Integer.MAX_VALUE, 5,
                TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
        Future future = pool.submit(myCallable);
        Thread.sleep(4000);
        System.out.println("cancel:" + future.cancel(true) + ",isCancelled:" + future.isCancelled());

    }
}


1.4 测试结果


运行中
运行中
运行中
运行中
运行中
运行中
cancel:true,isCancelled:true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值