Java多線程之Future設(shè)計模式
Future -> 代表的是未來的一個憑據(jù)
public interface Future<T> {
T get() throws InterruptedException;
}
AsynFuture -> Future具體實現(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 {
/**
* 需進程等待
*/
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;
}
/**
* 運行完 自動回調(diào)
* 無需進程等待
*/
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)用邏輯進行了隔離
public interface FutureTask<T> {
T call();
}
需要時回調(diào):
/**
* Future -> 代表的是未來的一個憑據(jù)
* FutureTask -> 將你的調(diào)用邏輯進行了隔離
* 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());
}
}
運行:
==============
do other thing.
==============
FINISH
運行完自動回調(diào):
//**
* Future -> 代表的是未來的一個憑據(jù)
* FutureTask -> 將你的調(diào)用邏輯進行了隔離
* 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("==============");
}
}
到此這篇關(guān)于Java多線程之Future設(shè)計模式的文章就介紹到這了,更多相關(guān)Java多線程 Future內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot中rabbitmq實現(xiàn)消息可靠性機制詳解
這篇文章主要介紹了springboot中rabbitmq實現(xiàn)消息可靠性機制詳解,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2021-09-09
Mybatis-Plus自動填充更新操作相關(guān)字段的實現(xiàn)
數(shù)據(jù)庫表中應該都要有create_time、update_time字段;那么在開發(fā)中,對于這些共有字段的處理應該要進行統(tǒng)一,這樣就可以簡化我們的開發(fā)過程。那么本文就對Mybatis-Plus中的字段自動填充進行記錄2021-11-11
springboot開發(fā)擴展springmvc實現(xiàn)解析
這篇文章主要介紹了springboot開發(fā)擴展springmvc實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02
java.exe和javaw.exe的區(qū)別及使用方法
這篇文章主要介紹了java.exe和javaw.exe的區(qū)別及使用方法,需要的朋友可以參考下2014-04-04

