Java的MoreSuppliers工具类方法解析
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工具类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringBoot 配合 SpringSecurity 实现自动登录功能的代码
这篇文章主要介绍了SpringBoot 配合 SpringSecurity 实现自动登录功能的代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-09-09
MybatisPlus自带的queryWrapper实现时间倒序方式
这篇文章主要介绍了MybatisPlus自带的queryWrapper实现时间倒序方式,具有很好的参考价值,希望对的有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-01-01
Java多线程并发的指令重排序问题及volatile写屏障原理详解
这篇文章主要介绍了Java多线程并发的指令重排序问题及volatile写屏障原理详解,指令重排序是编译器或处理器为了提高性能而对指令执行顺序进行重新排列的优化技术,需要的朋友可以参考下2024-01-01


最新评论