guava 缓存的 removalListener 是否在单独的线程中运行?
Is removalListener of guava cache runs in a separate thread?
我想在从 guava 缓存中删除对象时执行一些清理。
但是我需要过一段时间再做。
我可以睡在那里吗?
它会阻塞所有线程吗?
或者 removalListener 在单独的线程中运行?
CacheBuilder.newBuilder().
.removalListener(notification -> {
try {
Thread.sleep(10 * 60 * 1000);
} catch (InterruptedException e) {
}
try {
//close
} catch (final IOException e) {
}
})
.build();
来自Removal Listeners · CachesExplained · google/guava Wiki:
Warning: removal listener operations are executed synchronously by default, and since cache maintenance is normally performed during normal cache operations, expensive removal listeners can slow down normal cache function! If you have an expensive removal listener, use RemovalListeners.asynchronous(RemovalListener, Executor)
to decorate a RemovalListener
to operate asynchronously.
例如
Executor executor = Executors.newFixedThreadPool(10);
CacheBuilder.newBuilder()
.removalListener(RemovalListeners.asynchronous(notification -> {
Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MINUTES);
try {
//close
} catch (final IOException e) {
}
}, executor))
.build();
我想在从 guava 缓存中删除对象时执行一些清理。 但是我需要过一段时间再做。 我可以睡在那里吗? 它会阻塞所有线程吗? 或者 removalListener 在单独的线程中运行?
CacheBuilder.newBuilder().
.removalListener(notification -> {
try {
Thread.sleep(10 * 60 * 1000);
} catch (InterruptedException e) {
}
try {
//close
} catch (final IOException e) {
}
})
.build();
来自Removal Listeners · CachesExplained · google/guava Wiki:
Warning: removal listener operations are executed synchronously by default, and since cache maintenance is normally performed during normal cache operations, expensive removal listeners can slow down normal cache function! If you have an expensive removal listener, use
RemovalListeners.asynchronous(RemovalListener, Executor)
to decorate aRemovalListener
to operate asynchronously.
例如
Executor executor = Executors.newFixedThreadPool(10);
CacheBuilder.newBuilder()
.removalListener(RemovalListeners.asynchronous(notification -> {
Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MINUTES);
try {
//close
} catch (final IOException e) {
}
}, executor))
.build();