Java多線程15 Future設(shè)計(jì)模式

Java多線程目錄

Future -> 代表的是未來的一個(gè)憑據(jù)

public interface Future<T> {
    T get() throws InterruptedException;
}

AsynFuture -> Future具體實(shí)現(xiàn)類

public class AsynFuture<T> implements Future<T> {

    private volatile boolean done = false;

    private T result;

    public void done(T result){
        synchronized (this){
            this.result = result;
            this.done = true;
            this.notifyAll();
        }
    }
    /**
     * 輪詢 沒有完成等待
     */
    @Override
    public T get() throws InterruptedException {
        synchronized (this) {
            while (!done) {
                this.wait();
            }
        }
        return result;
    }
}

FutureService -> 橋接Future和FutureTask

public class FutureService {

    /**
     * 需進(jìn)程等待
     */
    public <T> Future<T> submit(final FutureTask<T> task) {

        AsynFuture<T> asynFuture = new AsynFuture<>();

        new Thread(() -> {

            T result = task.call();
            asynFuture.done(result);

        }).start();
        return asynFuture;
    }

    /**
     * 運(yùn)行完 自動回調(diào)
     * 無需進(jìn)程等待
     */
    public <T> Future<T> submit(final FutureTask<T> task, final Consumer<T> consumer) {

        AsynFuture<T> asynFuture = new AsynFuture<>();
        new Thread(() -> {
            T result = task.call();
            asynFuture.done(result);
            consumer.accept(result);
        }).start();
        return asynFuture;
    }
}

FutureTask -> 將你的調(diào)用邏輯進(jìn)行了隔離

public interface FutureTask<T> {

    T call();
}

需要時(shí)回調(diào)

/**
 * Future        -> 代表的是未來的一個(gè)憑據(jù)
 * FutureTask    -> 將你的調(diào)用邏輯進(jìn)行了隔離
 * FutureService -> 橋接Future和FutureTask
 */
public class SyncInvoker {

    public static void main(String[] args) throws InterruptedException {

        FutureService futureService = new FutureService();
        Future<String> future = futureService.submit(() -> {
            try {
                Thread.sleep(10001);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "FINISH";
        });

        System.out.println("==============");
        System.out.println("do other thing.");
        Thread.sleep(1000);

        System.out.println("==============");

        /**
         * 調(diào)用也形成了阻塞
         */
        System.out.println(future.get());
    }
}

運(yùn)行

==============
do other thing.
==============
FINISH

運(yùn)行完自動回調(diào)

//**
 * Future        -> 代表的是未來的一個(gè)憑據(jù)
 * FutureTask    -> 將你的調(diào)用邏輯進(jìn)行了隔離
 * FutureService -> 橋接Future和FutureTask
 */
public class SyncInvoker {

    public static void main(String[] args) throws InterruptedException {

        FutureService futureService = new FutureService();
        futureService.submit(() -> {
            try {
                Thread.sleep(10001);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "FINISH";
        },System.out::println);

        System.out.println("==============");
        System.out.println("do other thing.");
        Thread.sleep(1000);
        System.out.println("==============");
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 進(jìn)程和線程 進(jìn)程 所有運(yùn)行中的任務(wù)通常對應(yīng)一個(gè)進(jìn)程,當(dāng)一個(gè)程序進(jìn)入內(nèi)存運(yùn)行時(shí),即變成一個(gè)進(jìn)程.進(jìn)程是處于運(yùn)行過程中...
    勝浩_ae28閱讀 5,257評論 0 23
  • 進(jìn)程和線程 進(jìn)程 所有運(yùn)行中的任務(wù)通常對應(yīng)一個(gè)進(jìn)程,當(dāng)一個(gè)程序進(jìn)入內(nèi)存運(yùn)行時(shí),即變成一個(gè)進(jìn)程.進(jìn)程是處于運(yùn)行過程中...
    小徐andorid閱讀 2,993評論 3 53
  • 該文章轉(zhuǎn)自:http://blog.csdn.net/evankaka/article/details/44153...
    加來依藍(lán)閱讀 7,470評論 3 87
  • 在Java中,使用線程來異步執(zhí)行任務(wù)。Java線程的創(chuàng)建與銷毀需要一定的開銷,如果我們?yōu)槊恳粋€(gè)任務(wù)創(chuàng)建一個(gè)新線程來...
    Steven1997閱讀 852評論 0 0
  • 黃河!像一個(gè)巨人出現(xiàn)在亞洲平原之上,你用那英雄的體魄筑成我們民族的屏障。我強(qiáng)大的母親黃河啊!
    王錟閱讀 206評論 0 1

友情鏈接更多精彩內(nèi)容