最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java的MoreSuppliers工具類方法解析

 更新時(shí)間:2024年01月16日 08:50:27   作者:加油當(dāng)當(dāng)  
這篇文章主要介紹了Java的MoreSuppliers工具類方法解析,MoreSuppliers類是一個(gè)Java工具類,它提供了一些增強(qiáng)的Supplier函數(shù),使得Supplier執(zhí)行的結(jié)果可以被緩存,真正的調(diào)用只執(zhí)行一次,需要的朋友可以參考下

MoreSuppliers工具類

MoreSuppliers類是一個(gè)Java工具類,它提供了一些增強(qiáng)的Supplier函數(shù),使得Supplier執(zhí)行的結(jié)果可以被緩存,真正的調(diào)用只執(zhí)行一次。

public static <T> CloseableSupplier<T> lazy(Supplier<T> delegate):

這個(gè)方法返回一個(gè)懶加載的提供器,首次獲取值時(shí)通過(guò)delegate加載值,并緩存這個(gè)值,在后續(xù)獲取時(shí)直接返回這個(gè)緩存的值。這個(gè)方法的使用場(chǎng)景是當(dāng)你有一個(gè)計(jì)算成本較高或者IO操作的Supplier,并且你希望只執(zhí)行一次這個(gè)操作,然后緩存結(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):

這個(gè)方法和上一個(gè)方法類似,但是它允許在關(guān)閉提供器返回的資源后,是否釋放緩存的對(duì)象。這個(gè)方法的使用場(chǎng)景是當(dāng)你的Supplier返回的是一個(gè)需要關(guān)閉的資源,比如一個(gè)數(shù)據(jù)庫(kù)連接,你希望在關(guān)閉這個(gè)資源后,下次調(diào)用get()方法時(shí)重新獲取一個(gè)新的資源。

示例:

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):

這個(gè)方法返回一個(gè)懶加載的提供器,支持異常類型聲明。這個(gè)方法的使用場(chǎng)景是當(dāng)你的Supplier可能拋出一個(gè)異常,你希望這個(gè)異常能被正確地傳播出去。

示例:

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):

這個(gè)方法和上一個(gè)方法類似,但是它允許在關(guān)閉提供器返回的資源后,是否釋放緩存的對(duì)象。這個(gè)方法的使用場(chǎng)景是當(dāng)你的Supplier返回的是一個(gè)需要關(guān)閉的資源并且可能拋出一個(gè)異常,你希望在關(guān)閉這個(gè)資源后,下次調(diào)用get()方法時(shí)重新獲取一個(gè)新的資源,并且異常能被正確地傳播出去。

示例:

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):

這個(gè)方法返回一個(gè)異步加載的提供器,通過(guò)異步線程來(lái)完成初始化操作,支持超時(shí)。當(dāng)超過(guò)指定的時(shí)間沒(méi)有獲取初始值成功時(shí),使用pendingSupplier提供的值作為托底。這個(gè)方法的使用場(chǎng)景是當(dāng)你的Supplier需要花費(fèi)較長(zhǎng)的時(shí)間來(lái)獲取值,你希望這個(gè)操作能在一個(gè)單獨(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):

這個(gè)方法和上一個(gè)方法類似,但是它沒(méi)有提供托底的Supplier,如果異步初始化值超時(shí),它將返回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):

這個(gè)方法和上一個(gè)方法類似,但是它沒(méi)有指定執(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>:

這是一個(gè)可關(guān)閉的Supplier實(shí)現(xiàn),支持通過(guò)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>:

這是一個(gè)可關(guān)閉的Supplier實(shí)現(xiàn),支持異常類型聲明,通過(guò)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>:

這是一個(gè)異步加載的Supplier實(shí)現(xiàn),通過(guò)異步線程來(lái)完成初始化操作,支持超時(shí)。當(dāng)超過(guò)指定的時(shí)間沒(méi)有獲取初始值成功時(shí),使用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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • jedis的testWhileIdle用法源碼解讀

    jedis的testWhileIdle用法源碼解讀

    這篇文章主要為大家介紹了jedis的testWhileIdle用法源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • SpringBoot 配合 SpringSecurity 實(shí)現(xiàn)自動(dòng)登錄功能的代碼

    SpringBoot 配合 SpringSecurity 實(shí)現(xiàn)自動(dòng)登錄功能的代碼

    這篇文章主要介紹了SpringBoot 配合 SpringSecurity 實(shí)現(xiàn)自動(dòng)登錄功能的代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java對(duì)象轉(zhuǎn)換的實(shí)現(xiàn)方式匯總

    Java對(duì)象轉(zhuǎn)換的實(shí)現(xiàn)方式匯總

    這篇文章主要介紹了Java對(duì)象轉(zhuǎn)換的多種實(shí)現(xiàn)方式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2025-04-04
  • MybatisPlus自帶的queryWrapper實(shí)現(xiàn)時(shí)間倒序方式

    MybatisPlus自帶的queryWrapper實(shí)現(xiàn)時(shí)間倒序方式

    這篇文章主要介紹了MybatisPlus自帶的queryWrapper實(shí)現(xiàn)時(shí)間倒序方式,具有很好的參考價(jià)值,希望對(duì)的有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java多線程并發(fā)的指令重排序問(wèn)題及volatile寫屏障原理詳解

    Java多線程并發(fā)的指令重排序問(wèn)題及volatile寫屏障原理詳解

    這篇文章主要介紹了Java多線程并發(fā)的指令重排序問(wèn)題及volatile寫屏障原理詳解,指令重排序是編譯器或處理器為了提高性能而對(duì)指令執(zhí)行順序進(jìn)行重新排列的優(yōu)化技術(shù),需要的朋友可以參考下
    2024-01-01
  • SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

    SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

    這篇文章主要介紹了SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • JavaWeb工程中集成YMP框架快速上手(二)

    JavaWeb工程中集成YMP框架快速上手(二)

    YMP是一個(gè)非常簡(jiǎn)單、易用的一套輕量級(jí)JAVA應(yīng)用開發(fā)框架,設(shè)計(jì)原則主要側(cè)重于簡(jiǎn)化工作任務(wù)、規(guī)范開發(fā)流程、提高開發(fā)效率。對(duì)YMP框架感興趣的小伙伴們可以參考一下
    2016-02-02
  • 詳解Java的線程狀態(tài)

    詳解Java的線程狀態(tài)

    本文主要為大家詳細(xì)介紹一下Java的線程狀態(tài),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的幫助,感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下
    2022-11-11
  • Java實(shí)現(xiàn)導(dǎo)出ZIP壓縮包的方法

    Java實(shí)現(xiàn)導(dǎo)出ZIP壓縮包的方法

    這篇文章主要介紹了Java實(shí)現(xiàn)導(dǎo)出ZIP壓縮包的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Java數(shù)據(jù)結(jié)構(gòu)二叉樹難點(diǎn)解析

    Java數(shù)據(jù)結(jié)構(gòu)二叉樹難點(diǎn)解析

    樹是一種重要的非線性數(shù)據(jù)結(jié)構(gòu),直觀地看,它是數(shù)據(jù)元素(在樹中稱為結(jié)點(diǎn))按分支關(guān)系組織起來(lái)的結(jié)構(gòu),很象自然界中的樹那樣。樹結(jié)構(gòu)在客觀世界中廣泛存在,如人類社會(huì)的族譜和各種社會(huì)組織機(jī)構(gòu)都可用樹形象表示
    2021-10-10

最新評(píng)論

东乡| 乐亭县| 大兴区| 承德县| 岑溪市| 崇明县| 沾益县| 六盘水市| 龙南县| 福州市| 乐昌市| 虹口区| 沧州市| 台江县| 宜良县| 长乐市| 曲沃县| 稻城县| 肥东县| 庄浪县| 卢湾区| 霍城县| 资溪县| 华宁县| 五寨县| 大悟县| 都兰县| 同江市| 武穴市| 广宁县| 毕节市| 龙陵县| 大城县| 西乌珠穆沁旗| 拜城县| 朔州市| 池州市| 灵璧县| 西华县| 沂南县| 富阳市|