欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java使用Runnable和Callable實(shí)現(xiàn)多線程的區(qū)別詳解

 更新時(shí)間:2022年07月07日 11:39:19   作者:知識(shí)焦慮癥患者  
這篇文章主要為大家詳細(xì)介紹了Java使用Runnable和Callable實(shí)現(xiàn)多線程的區(qū)別之處,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下

使用Runnable和Callable接口實(shí)現(xiàn)多線程的區(qū)別

先看兩種實(shí)現(xiàn)方式的步驟:

1.實(shí)現(xiàn)Runnable接口

public class ThreadDemo{
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
             //創(chuàng)建并啟動(dòng)由實(shí)現(xiàn)Runnable接口創(chuàng)建的線程
            new Thread(new Runner(),"Thread"+i).start();
        }
    }
}

//實(shí)現(xiàn)Runnable接口
class Runner implements Runnable{
    //實(shí)現(xiàn)run方法
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"有實(shí)現(xiàn)Runnable接口創(chuàng)建");
    }
}

2.實(shí)現(xiàn) Callable 接口

public class ThreadDemo{
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        for (int i = 1; i <= 5; i++) {
            //使用FutureTask保存線程結(jié)果
            FutureTask<String> futureTask = new FutureTask<String>(new Caller());
            //創(chuàng)建并啟動(dòng)由實(shí)現(xiàn)Callable創(chuàng)建的線程
            new Thread(futureTask,"Thread"+i).start();
            //獲取線程執(zhí)行結(jié)果
            System.out.println(futureTask.get());
        }
    }
}

//實(shí)現(xiàn)Callable接口
class Caller implements Callable{
    //實(shí)現(xiàn)Call接口
    @Override
    public Object call() throws Exception {
        String result = Thread.currentThread().getName()+"由實(shí)現(xiàn)Callable接口創(chuàng)建";
        return result;
    }
}

從以上代碼可以看出,使用 Callable 接口創(chuàng)建多線程時(shí)使用了 FutureTask 進(jìn)行封裝,然后 FutureTask 作為參數(shù)傳給 Thread 的構(gòu)造函數(shù),而 FutureTask 的作用是存放 Callable 接口 call 方法的返回值。我們來看一下 FutureTask 的源碼

//FutureTask實(shí)現(xiàn)了RunnableFuture接口
public class FutureTask<V> implements RunnableFuture<V> {}

//再看RunnableFuture接口,繼承了Runnable接口
public interface RunnableFuture<V> extends Runnable, Future<V> {
    void run();
}

//回到FutureTask,找到run方法
public void run() {
    ...
    Callable<V> c = callable;
    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);
    }
    ...
}

//看一下set方法
protected void set(V v) {
    if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
        outcome = v;
        ...
    }
}

//再看一下FutureTask的構(gòu)造函數(shù)
public FutureTask(Callable<V> callable) {
        if (callable == null)
            throw new NullPointerException();
        this.callable = callable;
        this.state = NEW;
}

從對 FutureTask 的源碼分析,我們可以看出 FutureTask 實(shí)現(xiàn)了 Runnable 接口的 run 方法,在 run 方法中調(diào)用了 Callable 的 call 方法,將結(jié)果保存到 result 中,通過 set 方法將結(jié)果存儲(chǔ)至 outcome 變量中。

通過以上分析,我們總結(jié)出使用 Runnable 和 Callable 接口的區(qū)別:

1.使用 Runnable 接口實(shí)現(xiàn)更加簡單,而 通過 Callable 接口創(chuàng)建線程需要 FutureTask 進(jìn)行封裝;

2.通過實(shí)現(xiàn) Runnable 接口創(chuàng)建的線程沒有返回值,而使用 Callable 接口創(chuàng)建的線程可以有返回結(jié)果,并保存在 FutureTask中;

3.通過實(shí)現(xiàn) Runnable 接口創(chuàng)建線程不拋出異常,而使用 Callable 接口創(chuàng)建的線程會(huì)拋出異常;

從以上總結(jié)可以看出,我們也可以看出 Runnable 適用于無需返回值的場景,而 Callable 接口用于需要保存返回值的場景。

到此這篇關(guān)于Java使用Runnable和Callable實(shí)現(xiàn)多線程的區(qū)別詳解的文章就介紹到這了,更多相關(guān)Java Runnable Callable多線程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論