国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java多線程之Future設計模式

Java多線程之Future設計模式

2022-03-04 18:01冬日毛毛雨 Java教程

這篇文章主要介紹了Java多線程之Future設計模式,Future 代表的是未來的一個憑據,文章主要附上Future具體實現類、橋接Future和FutureTask的代碼,需要的朋友可以參考一下

Future -> 代表的是未來的一個憑據

?
1
2
3
public interface Future<T> {
    T get() throws InterruptedException;
}

AsynFuture -> Future具體實現類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class FutureService {
 
    /**
     * 需進程等待
     */
    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;
    }
 
    /**
     * 運行完 自動回調
     * 無需進程等待
     */
    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 -> 將你的調用邏輯進行了隔離

?
1
2
3
4
public interface FutureTask<T> {
 
    T call();
}

需要時回調:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * Future        -> 代表的是未來的一個憑據
 * FutureTask    -> 將你的調用邏輯進行了隔離
 * 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("==============");
 
        /**
         * 調用也形成了阻塞
         */
        System.out.println(future.get());
    }
}

運行:

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

運行完自動回調:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//**
 * Future        -> 代表的是未來的一個憑據
 * FutureTask    -> 將你的調用邏輯進行了隔離
 * 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("==============");
    }
}

到此這篇關于Java多線程之Future設計模式的文章就介紹到這了,更多相關Java多線程 Future內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://juejin.cn/post/7022215197204938783

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧洲亚洲精品久久久久 | 欧美簧片在线 | 亚洲成人免费网站 | 日韩欧美专区 | 日韩精品久久 | 亚洲国产欧美日韩 | 99免费在线视频 | 视频一区二区三区在线观看 | 亚洲欧美激情精品一区二区 | 亚洲不卡高清视频 | av不卡电影在线观看 | 媚黑视频 | 中文字幕一区在线观看视频 | 国产欧美中文字幕 | 亚洲视频1区 | 国内成人免费视频 | 精品在线91 | 成人在线免费网站 | 日韩一二区 | 亚洲精品一区二三区 | 午夜电影一区 | 国产综合网站 | 精品一区二区三区中文字幕 | 国外精品久久久蜜桃免费全文阅读 | 久久久久国产一区二区三区四区 | 日本电影一区 | 久久一区二区视频 | 国产精品久久久久久久一区探花 | 999精品视频 | 中文字幕第一区 | 国产在线综合视频 | 69免费视频 | 欧美精品在线看 | 国产精品美乳一区二区免费 | 欧美日韩亚洲一区二区 | 国产特级毛片aaaaaaa高清 | 久久人人爽爽爽人久久久 | 日韩免费 | 久草中文在线 | 女人久久久久 | 免费在线看a |