Java多線程之Future設計模式
更新時間:2021年10月28日 11:29:59 作者:冬日毛毛雨
這篇文章主要介紹了Java多線程之Future設計模式,Future 代表的是未來的一個憑據,文章主要附上Future具體實現類、橋接Future和FutureTask的代碼,需要的朋友可以參考一下
Future -> 代表的是未來的一個憑據
public interface Future<T> {
T get() throws InterruptedException;
}
AsynFuture -> Future具體實現類
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;
}
/**
* 運行完 自動回調
* 無需進程等待
*/
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 -> 將你的調用邏輯進行了隔離
public interface FutureTask<T> {
T call();
}
需要時回調:
/**
* 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
運行完自動回調:
//**
* 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內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中的ReadWriteLock高效處理并發(fā)讀寫操作實例探究
這篇文章主要為大家介紹了Java中的ReadWriteLock高效處理并發(fā)讀寫操作實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
MyBatis 執(zhí)行動態(tài) SQL語句詳解
大家對mybatis執(zhí)行任意sql語句都了解,那么MyBatis執(zhí)行動態(tài)SQL語句呢?下面腳本之家小編給大家解答下mybatis執(zhí)行動態(tài)sql語句的方法,非常不錯,感興趣的朋友參考下吧2016-08-08
關于JDK8升級17及springboot?2.x升級3.x詳細指南
這篇文章主要介紹了關于JDK8升級17及springboot?2.x升級3.x的相關資料,還討論了JPA包路徑從javax改為jakarta,以及Spring?Boot版本升級和Redis配置調整等,需要的朋友可以參考下2025-01-01
如何自定義Jackson序列化?@JsonSerialize
這篇文章主要介紹了如何自定義Jackson序列化?@JsonSerialize,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

