Java线程的创建方式三:Callable(四)

本文详细介绍了Java中实现多线程的第三种方式:通过实现Callable接口并覆盖call方法,以及如何使用Future接口获取线程执行结果。探讨了Callable和Future的出现原因,及其在多线程编程中的作用。

一、Java实现多线程的三种方式

方式一:继承Thread类:

public class Test extends Thread {
    public static void main(String[] args) {
        Thread t = new Test();
        t.start();
    }
    @Override
    public void run() {
        System.out.println("Override run() ...");
    }
}

方式二:实现Runnable接口,并覆写run方法:

public class Test implements Runnable {
    public static void main(String[] args) {
        Thread t = new Thread(new Test());
        t.start();
    }
    @Override
    public void run() {
        System.out.println("Override run() ...");
    }
}

我们今天来学习另外一种创建多线程的方式:实现Callable接口,并覆call方法

方式三:实现Callable接口,并覆call方法:

//1.创建一个线程类,实现Callable接口,
//2.Callable会报一个黄色的警告,原因:可以加个泛型,这个泛型,是返回值对应的类型。
//3.我们这个题是产生随机数,所以加个泛型Integer
public class RanDomCallable implements Callable<Integer> {
    //4.一旦上面的泛型确定了,那么这个重写的方法的返回值类型就是Integer了。
    public Integer call() throws Exception {
        // 睡眠2秒
        Thread.sleep(2000);
        return new Random().nextInt(10);
    }
    //5.写main方法测试
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        //6.创建一个线程对象:
        RanDomCallable rdc=new RanDomCallable();
        //7.启动线程,直接用Thread传rdc不行,必须再借助一个FutureTask
        FutureTask<Integer> ft=new FutureTask<Integer>(rdc);
        Thread t=new Thread(ft);//FutureTask实现了Runnable接口所以可以传入
        t.start();
        //8.上面已经将线程启动了,直接运行是没有结果的
        //9.我们必须要对返回值处理,那么如何接收返回值:
        Integer i = ft.get();//get方法中可以加入sleep,验证get是个阻塞方法
        //10.判断线程是否执行结束:
        System.out.println(ft.isDone());
        System.out.println(i);
        System.out.println(ft.isDone());
    }
}

上面代码的执行结果是:每两秒输出一个随机数,原因就是get方法是阻塞的啊,要执行完才会得到结果的。

我们看看FutureTask的源码:

(1)属性

// 底层线程执行的状态标识,具体的数值就是下面int类型的数量
private volatile int state;
// 正常的状态只有用到0,1,2,程序从0->1->2线程就执行完了
// 线程走完,最终state的数值,从1(new)变为2(completing)变为3(normal) 那么线程就执行好了!
private static final int NEW          = 0;
private static final int COMPLETING   = 1;
private static final int NORMAL       = 2;
private static final int EXCEPTIONAL  = 3;
private static final int CANCELLED    = 4;
private static final int INTERRUPTING = 5;
private static final int INTERRUPTED  = 6;

/** The underlying callable; nulled out after running */
// 要执行的任务
private Callable<V> callable;
/** The result to return or exception to throw from get() */
// 返回值存储在这里
private Object outcome; // non-volatile, protected by state reads/writes
/** The thread running the callable; CASed during run() */
// 执行任务的线程
private volatile Thread runner;
/** Treiber stack of waiting threads */
private volatile WaitNode waiters;

(2)构造方法

public FutureTask(Callable<V> callable) {
    if (callable == null)
        throw new NullPointerException();
    this.callable = callable;
    // 初始状态state = 0
    this.state = NEW;       // ensure visibility of callable
}

(3)run方法

public void run() {
        if (state != NEW ||
            !UNSAFE.compareAndSwapObject(this, runnerOffset,
                                         null, Thread.currentThread()))
            return;
        try {
            Callable<V> c = callable;
            // 初始状态时state == NEW == 0
            if (c != null && state == NEW) {
                V result;
                boolean ran;
                try {
                    // 调用call()
                    result = c.call();
                    ran = true;
                } catch (Throwable ex) {
                    result = null;
                    ran = false;
                    setException(ex);
                }
                // 若上面没有异常就会走这个方法
                if (ran)
                    set(result);
            }
        } finally {
            // runner must be non-null until state is settled to
            // prevent concurrent calls to run()
            runner = null;
            // state must be re-read after nulling runner to prevent
            // leaked interrupts
            int s = state;
            if (s >= INTERRUPTING)
                handlePossibleCancellationInterrupt(s);
        }
}

(4)set方法

protected void set(V v) {
        // 状态从0->1(COMPLETING) 
        if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
            // 返回值放入outcome中
            outcome = v;
            // 状态从1->2(NORMAL) 
            UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
            finishCompletion();
        }
}

(5)get方法

public V get() throws InterruptedException, ExecutionException {
        int s = state;
        // 如果状态是<=1的,那么,就进入awaitDone(死循环)
        // 什么时候状态变更为2,什么时候return
        // 所以会阻塞在这里,要一直等待状态变为2
        if (s <= COMPLETING)
            s = awaitDone(false, 0L);
        // 调用report方法,传入state = 2
        return report(s);
}
@SuppressWarnings("unchecked")
    private V report(int s) throws ExecutionException {
        Object x = outcome;
        // 只有当state == NORMAL,才会将返回值返回
        if (s == NORMAL)
            return (V)x;
        if (s >= CANCELLED)
            throw new CancellationException();
        throw new ExecutionException((Throwable)x);
}

二、Callable和Future出现的原因

创建线程的2种方式:

一种是直接继承Thread;

另外一种就是实现Runnable接口。

​ 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果。如果需要获取执行结果,就必须通过共享变量或者使用线程通信的方式来达到效果,这样使用起来就比较麻烦。从Java 1.5开始提供了Callable和Future两个接口,通过使用它们可以在任务执行完毕后得到执行结果。

三、Callable和Future介绍

Callable接口代表一段可以调用并返回结果的代码;

Future接口表示异步任务,是还没有完成的任务给出的未来结果。

所以说Callable用于产生结果,Future用于获取结果。

Callable接口使用泛型去定义它的返回类型。Executors类提供了一些有用的方法在线程池中执行Callable内的任务。由于Callable任务是并行的(并行就是整体看上去是并行的,其实在某个时间点只有一个线程在执行),我们必须等待它返回的结果。
java.util.concurrent.Future对象为我们解决了这个问题。在线程池提交Callable任务后返回了一个Future对象,使用它可以知道Callable任务的状态和得到Callable返回的执行结果。Future提供了get()方法(阻塞的方法)让我们可以等待Callable结束并获取它的执行结果。

也就是说Future提供了三种功能:

  1)判断任务是否完成;

  2)能够中断任务;

  3)能够获取任务执行结果。

  因为Future只是一个接口,所以是无法直接用来创建对象使用的,因此就有了下面的FutureTask,FutureTask实现了RunnableFuture接口。FutureTask已经在上面介绍过了,这里就不赘述了。

参考链接

本片文章,主要整理自互联网,便于自己复习知识所用,以下为参考链接!

【1】Java程序员必须掌握的线程知识-Callable和Future

【2】Callable原理,线程池执行Callable任务
【3】Java多线程之Callable接口及线程池

转载于:https://www.cnblogs.com/ch-forever/p/10218962.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值