Java的MoreSuppliers工具类方法解析

 更新时间:2024年01月16日 08:50:27   作者:加油当当  
这篇文章主要介绍了Java的MoreSuppliers工具类方法解析,MoreSuppliers类是一个Java工具类,它提供了一些增强的Supplier函数,使得Supplier执行的结果可以被缓存,真正的调用只执行一次,需要的朋友可以参考下

MoreSuppliers工具类

MoreSuppliers类是一个Java工具类,它提供了一些增强的Supplier函数,使得Supplier执行的结果可以被缓存,真正的调用只执行一次。

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

这个方法返回一个懒加载的提供器,首次获取值时通过delegate加载值,并缓存这个值,在后续获取时直接返回这个缓存的值。这个方法的使用场景是当你有一个计算成本较高或者IO操作的Supplier,并且你希望只执行一次这个操作,然后缓存结果以供后续使用。

示例:

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

这个方法和上一个方法类似,但是它允许在关闭提供器返回的资源后,是否释放缓存的对象。这个方法的使用场景是当你的Supplier返回的是一个需要关闭的资源,比如一个数据库连接,你希望在关闭这个资源后,下次调用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):

这个方法返回一个懒加载的提供器,支持异常类型声明。这个方法的使用场景是当你的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):

这个方法和上一个方法类似,但是它允许在关闭提供器返回的资源后,是否释放缓存的对象。这个方法的使用场景是当你的Supplier返回的是一个需要关闭的资源并且可能抛出一个异常,你希望在关闭这个资源后,下次调用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):

这个方法返回一个异步加载的提供器,通过异步线程来完成初始化操作,支持超时。当超过指定的时间没有获取初始值成功时,使用pendingSupplier提供的值作为托底。这个方法的使用场景是当你的Supplier需要花费较长的时间来获取值,你希望这个操作能在一个单独的线程中进行,而主线程可以继续执行其他任务。

示例:

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

这个方法和上一个方法类似,但是它没有指定执行初始化操作的线程名称。

示例:

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

这是一个可关闭的Supplier实现,支持通过tryClose(ThrowableConsumer<T, X>closer)方法关闭提供器返回的资源。

示例:

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

这是一个可关闭的Supplier实现,支持异常类型声明,通过tryClose(ThrowableConsumer<T, X> closer)方法关闭提供器返回的资源。

示例:

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实现,通过异步线程来完成初始化操作,支持超时。当超过指定的时间没有获取初始值成功时,使用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.

到此这篇关于Java的MoreSuppliers工具类方法解析的文章就介绍到这了,更多相关MoreSuppliers工具类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • jedis的testWhileIdle用法源码解读

    jedis的testWhileIdle用法源码解读

    这篇文章主要为大家介绍了jedis的testWhileIdle用法源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • SpringBoot 配合 SpringSecurity 实现自动登录功能的代码

    SpringBoot 配合 SpringSecurity 实现自动登录功能的代码

    这篇文章主要介绍了SpringBoot 配合 SpringSecurity 实现自动登录功能的代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • Java对象转换的实现方式汇总

    Java对象转换的实现方式汇总

    这篇文章主要介绍了Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2025-04-04
  • MybatisPlus自带的queryWrapper实现时间倒序方式

    MybatisPlus自带的queryWrapper实现时间倒序方式

    这篇文章主要介绍了MybatisPlus自带的queryWrapper实现时间倒序方式,具有很好的参考价值,希望对的有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Java多线程并发的指令重排序问题及volatile写屏障原理详解

    Java多线程并发的指令重排序问题及volatile写屏障原理详解

    这篇文章主要介绍了Java多线程并发的指令重排序问题及volatile写屏障原理详解,指令重排序是编译器或处理器为了提高性能而对指令执行顺序进行重新排列的优化技术,需要的朋友可以参考下
    2024-01-01
  • SpringBoot集成mqtt的多模块项目配置详解

    SpringBoot集成mqtt的多模块项目配置详解

    这篇文章主要介绍了SpringBoot集成mqtt的多模块项目配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • JavaWeb工程中集成YMP框架快速上手(二)

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

    YMP是一个非常简单、易用的一套轻量级JAVA应用开发框架,设计原则主要侧重于简化工作任务、规范开发流程、提高开发效率。对YMP框架感兴趣的小伙伴们可以参考一下
    2016-02-02
  • 详解Java的线程状态

    详解Java的线程状态

    本文主要为大家详细介绍一下Java的线程状态,文中的示例代码讲解详细,对我们学习有一定的帮助,感兴趣的小伙伴可以跟随小编学习一下
    2022-11-11
  • Java实现导出ZIP压缩包的方法

    Java实现导出ZIP压缩包的方法

    这篇文章主要介绍了Java实现导出ZIP压缩包的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Java数据结构二叉树难点解析

    Java数据结构二叉树难点解析

    树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构,很象自然界中的树那样。树结构在客观世界中广泛存在,如人类社会的族谱和各种社会组织机构都可用树形象表示
    2021-10-10

最新评论