溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java中Callable、FutureTask和Future接口的介紹

發(fā)布時間:2020-05-23 17:16:24 來源:億速云 閱讀:315 作者:鴿子 欄目:編程語言

一. Callable接口與Runnable接口區(qū)別

創(chuàng)建java線程,我們經(jīng)常使用兩種方式:

  • 一是直接繼承Thread
  • 另一種是實現(xiàn)Runnable接口

但這兩種方式有一個缺陷:在執(zhí)行完任務(wù)之后無法直接獲取執(zhí)行結(jié)果。

1. 接口定義

1.1 Callable接口
public interface Callable<V> {
    V call() throws Exception;
}
1.2 Runnable接口
public interface Runnable {
    public abstract void run();
}

2. 區(qū)別

  • Runnable沒有返回值;Callable可以返回執(zhí)行結(jié)果(泛型)。
  • Runnable異常只能在內(nèi)部處理,不能往上繼續(xù)拋出;Callable接口的call()方法允許拋出異常。
  • Callable需要配合FutureTask或Future使用。

二. Future接口和FutureTask實現(xiàn)類

1. Future接口定義了5個方法

public interface Future<V> {
    boolean cancel(boolean mayInterruptIfRunning);
    boolean isCancelled();
    boolean isDone();
    V get() throws InterruptedException, ExecutionException;
    V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}
  • cancel方法用來取消任務(wù),如果取消任務(wù)成功則返回true,如果取消任務(wù)失敗則返回false。參數(shù)mayInterruptIfRunning表示是否允許取消正在執(zhí)行卻沒有執(zhí)行完畢的任務(wù),如果設(shè)置true,則表示可以取消正在執(zhí)行過程中的任務(wù)。如果任務(wù)已經(jīng)完成,則無論mayInterruptIfRunning為true還是false,此方法肯定返回false,即如果取消已經(jīng)完成的任務(wù)會返回false;如果任務(wù)正在執(zhí)行,若mayInterruptIfRunning設(shè)置為true,則返回true,若mayInterruptIfRunning設(shè)置為false,則返回false;如果任務(wù)還沒有執(zhí)行,則無論mayInterruptIfRunning為true還是false,肯定返回true。
  • isCancelled方法表示任務(wù)是否被取消成功,如果在任務(wù)正常完成前被取消成功,則返回 true。
  • isDone方法表示任務(wù)是否已經(jīng)完成,若任務(wù)完成,則返回true;
  • get()方法用來獲取執(zhí)行結(jié)果,這個方法會產(chǎn)生阻塞,會一直等到任務(wù)執(zhí)行完畢才返回;
  • get(long timeout, TimeUnit unit)用來獲取執(zhí)行結(jié)果,如果在指定時間內(nèi),還沒獲取到結(jié)果,就直接返回null。

2. FutureTask實現(xiàn)了RunnableFuture接口,RunnableFuture繼承了Runnable接口和Future接口

public class FutureTask<V> implements RunnableFuture<V>
public interface RunnableFuture<V> extends Runnable, Future<V> {
    void run();
}

三. 基本用法舉例

1. Runnable

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        System.out.println("線程執(zhí)行中...");
    }
};
Thread thread = new Thread(runnable);
thread.start();

2. Callable

2.1 FutureTask
Callable < Integer > callable = new Callable < Integer > () {
    @Override
    public Integer call() throws Exception {
        System.out.println("線程執(zhí)行中...");
        return 100;
    }
};
FutureTask < Integer > futureTask = new FutureTask < Integer > (callable);
new Thread(futureTask).start();
// 等待1秒,讓線程執(zhí)行
TimeUnit.SECONDS.sleep(1);
if(futureTask.isDone()) {
    System.out.println("獲取執(zhí)行結(jié)果:" + futureTask.get());
}
2.2 Future
Callable < Integer > callable = new Callable < Integer > () {
    @Override
    public Integer call() throws Exception {
        System.out.println("線程執(zhí)行中...");
        return 100;
    }
};
ExecutorService service = Executors.newCachedThreadPool();
Future < Integer > future = service.submit(callable);
// 等待1秒,讓線程執(zhí)行
TimeUnit.SECONDS.sleep(1);
if(futureTask.isDone()) {
    System.out.println("獲取執(zhí)行結(jié)果:" + future.get());
}

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI