Java的MoreSuppliers工具類方法解析
MoreSuppliers工具類
MoreSuppliers類是一個Java工具類,它提供了一些增強(qiáng)的Supplier函數(shù),使得Supplier執(zhí)行的結(jié)果可以被緩存,真正的調(diào)用只執(zhí)行一次。
public static <T> CloseableSupplier<T> lazy(Supplier<T> delegate):
這個方法返回一個懶加載的提供器,首次獲取值時通過delegate加載值,并緩存這個值,在后續(xù)獲取時直接返回這個緩存的值。這個方法的使用場景是當(dāng)你有一個計(jì)算成本較高或者IO操作的Supplier,并且你希望只執(zhí)行一次這個操作,然后緩存結(jié)果以供后續(xù)使用。
示例:
Supplier<String> expensiveOperation = () -> { // Some expensive operation... return "result"; }; Supplier<String> lazySupplier = MoreSuppliers.lazy(expensiveOperation); String result = lazySupplier.get(); // The expensive operation is performed here. String cachedResult = lazySupplier.get(); // The cached result is returned here.
public static <T> CloseableSupplier<T> lazy(Supplier<T> delegate, boolean resetAfterClose):
這個方法和上一個方法類似,但是它允許在關(guān)閉提供器返回的資源后,是否釋放緩存的對象。這個方法的使用場景是當(dāng)你的Supplier返回的是一個需要關(guān)閉的資源,比如一個數(shù)據(jù)庫連接,你希望在關(guān)閉這個資源后,下次調(diào)用get()方法時重新獲取一個新的資源。
示例:
Supplier<Connection> connectionSupplier = () -> { // Get a connection from the database... return connection; }; CloseableSupplier<Connection> lazySupplier = MoreSuppliers.lazy(connectionSupplier, true); Connection connection = lazySupplier.get(); // The connection is obtained here. lazySupplier.tryClose(Connection::close); // The connection is closed here. Connection newConnection = lazySupplier.get(); // A new connection is obtained here.
public static <T, X extends Throwable> CloseableThrowableSupplier<T, X> lazyEx(ThrowableSupplier<T, X> delegate):
這個方法返回一個懶加載的提供器,支持異常類型聲明。這個方法的使用場景是當(dāng)你的Supplier可能拋出一個異常,你希望這個異常能被正確地傳播出去。
示例:
ThrowableSupplier<String, IOException> ioOperation = () -> { // Some IO operation... return "result"; }; ThrowableSupplier<String, IOException> lazySupplier = MoreSuppliers.lazyEx(ioOperation); try { String result = lazySupplier.get(); // The IO operation is performed here. } catch (IOException e) { // Handle the exception... }
public static <T, X extends Throwable> CloseableThrowableSupplier<T, X> lazyEx(ThrowableSupplier<T, X> delegate, boolean resetAfterClose):
這個方法和上一個方法類似,但是它允許在關(guān)閉提供器返回的資源后,是否釋放緩存的對象。這個方法的使用場景是當(dāng)你的Supplier返回的是一個需要關(guān)閉的資源并且可能拋出一個異常,你希望在關(guān)閉這個資源后,下次調(diào)用get()方法時重新獲取一個新的資源,并且異常能被正確地傳播出去。
示例:
ThrowableSupplier<Connection, SQLException> connectionSupplier = () -> { // Get a connection from the database... return connection; }; CloseableThrowableSupplier<Connection, SQLException> lazySupplier = MoreSuppliers.lazyEx(connectionSupplier, true); try { Connection connection = lazySupplier.get(); // The connection is obtained here. lazySupplier.tryClose(Connection::close); // The connection is closed here. Connection newConnection = lazySupplier.get(); // A new connection is obtained here. } catch (SQLException e) { // Handle the exception... }
public static <T> AsyncSupplier<T> asyncLazyEx(Supplier<T> delegate, Supplier<T> pendingSupplier, String threadName):
這個方法返回一個異步加載的提供器,通過異步線程來完成初始化操作,支持超時。當(dāng)超過指定的時間沒有獲取初始值成功時,使用pendingSupplier提供的值作為托底。這個方法的使用場景是當(dāng)你的Supplier需要花費(fèi)較長的時間來獲取值,你希望這個操作能在一個單獨(dú)的線程中進(jìn)行,而主線程可以繼續(xù)執(zhí)行其他任務(wù)。
示例:
Supplier<String> slowOperation = () -> { // Some slow operation... return "result"; }; Supplier<String> fallback = () -> "fallback"; AsyncSupplier<String> asyncSupplier = MoreSuppliers.asyncLazyEx(slowOperation, fallback, "InitThread"); String result = asyncSupplier.get(Duration.ofSeconds(5)); // The slow operation is performed in a separate thread. If it takes more than 5 seconds, the fallback value is returned.
public static <T> AsyncSupplier<T> asyncLazyEx(Supplier<T> delegate, String threadName):
這個方法和上一個方法類似,但是它沒有提供托底的Supplier,如果異步初始化值超時,它將返回null。
示例:
Supplier<String> slowOperation = () -> { // Some slow operation... return "result"; }; AsyncSupplier<String> asyncSupplier = MoreSuppliers.asyncLazyEx(slowOperation, "InitThread"); String result = asyncSupplier.get(Duration.ofSeconds(5)); // The slow operation is performed in a separate thread. If it takes more than 5 seconds, null is returned.
public static <T> AsyncSupplier<T> asyncLazyEx(Supplier<T> delegate):
這個方法和上一個方法類似,但是它沒有指定執(zhí)行初始化操作的線程名稱。
示例:
Supplier<String> slowOperation = () -> { // Some slow operation... return "result"; }; AsyncSupplier<String> asyncSupplier = MoreSuppliers.asyncLazyEx(slowOperation); String result = asyncSupplier.get(Duration.ofSeconds(5)); // The slow operation is performed in a separate thread. If it takes more than 5 seconds, null is returned.
CloseableSupplier<T>:
這是一個可關(guān)閉的Supplier實(shí)現(xiàn),支持通過tryClose(ThrowableConsumer<T, X>closer)方法關(guān)閉提供器返回的資源。
示例:
CloseableSupplier<Connection> connectionSupplier = MoreSuppliers.lazy(() -> { // Get a connection from the database... return connection; }, true); Connection connection = connectionSupplier.get(); // The connection is obtained here. connectionSupplier.tryClose(Connection::close); // The connection is closed here.
CloseableThrowableSupplier<T, X>:
這是一個可關(guān)閉的Supplier實(shí)現(xiàn),支持異常類型聲明,通過tryClose(ThrowableConsumer<T, X> closer)方法關(guān)閉提供器返回的資源。
示例:
CloseableThrowableSupplier<Connection, SQLException> connectionSupplier = MoreSuppliers.lazyEx(() -> { // Get a connection from the database... return connection; }, true); try { Connection connection = connectionSupplier.get(); // The connection is obtained here. connectionSupplier.tryClose(Connection::close); // The connection is closed here. } catch (SQLException e) { // Handle the exception... }
AsyncSupplier<T>:
這是一個異步加載的Supplier實(shí)現(xiàn),通過異步線程來完成初始化操作,支持超時。當(dāng)超過指定的時間沒有獲取初始值成功時,使用pendingSupplier提供的值作為托底。
示例:
Supplier<String> slowOperation = () -> { // Some slow operation... return "result"; }; Supplier<String> fallback = () -> "fallback"; AsyncSupplier<String> asyncSupplier = MoreSuppliers.asyncLazyEx(slowOperation, fallback, "InitThread"); String result = asyncSupplier.get(Duration.ofSeconds(5)); // The slow operation is performed in a separate thread. If it takes more than 5 seconds, the fallback value is returned.
到此這篇關(guān)于Java的MoreSuppliers工具類方法解析的文章就介紹到這了,更多相關(guān)MoreSuppliers工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot+springsecurity+mybatis+JWT+Redis?實(shí)現(xiàn)前后端離實(shí)戰(zhàn)教程
這篇文章主要介紹了springboot+springsecurity+mybatis+JWT+Redis?實(shí)現(xiàn)前后端離實(shí)戰(zhàn)教程,需要的朋友可以參考下2024-01-01SpringBoot實(shí)現(xiàn)發(fā)送電子郵件
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)發(fā)送電子郵件,電子郵件是—種用電子手段提供信息交換的通信方式,是互聯(lián)網(wǎng)應(yīng)用最廣的服務(wù)。通過網(wǎng)絡(luò)的電子郵件系統(tǒng),用戶可以非??焖俚姆绞?,與世界上任何一個角落的網(wǎng)絡(luò)用戶聯(lián)系,下面就來看看SpringBoot如何實(shí)現(xiàn)發(fā)送電子郵件吧2022-01-01spring整合atomikos實(shí)現(xiàn)分布式事務(wù)的方法示例
本文整合了一個spring和atomikos的demo,并且通過案例演示說明atomikos的作用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05java實(shí)現(xiàn)的導(dǎo)出Excel工具類實(shí)例
這篇文章主要介紹了java實(shí)現(xiàn)的導(dǎo)出Excel工具類,結(jié)合具體實(shí)例形式分析了java導(dǎo)出Excel導(dǎo)出并生成Excel表格相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-10-10MyBatisPlus中@TableField注解的基本使用
這篇文章主要介紹了MyBatisPlus中@TableField注解的基本使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Java加載本地庫的方法之System.load與System.loadLibrary
最近在做的工作要用到本地方法,所以下面這篇文章主要介紹了Java加載本地庫的方法之System.load與System.loadLibrary的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09Java實(shí)現(xiàn)求小于n的質(zhì)數(shù)的3種方法
這篇文章主要介紹了Java實(shí)現(xiàn)求小于n的質(zhì)數(shù)的3種方法,本文給出了根據(jù)定義去求解、平方根、找規(guī)律三種解法,需要的朋友可以參考下2015-03-03