番石榴缓存的removeListener是否在单独的线程中运行?

Mic*_*ael 1 guava google-guava-cache

当我从番石榴缓存中删除对象时,我想执行一些清理操作。但是我需要一段时间后再做。我可以在那里睡觉吗?会阻塞所有线程吗?还是removeListener在单独的线程中运行?

CacheBuilder.newBuilder().
.removalListener(notification -> {
    try {
        Thread.sleep(10 * 60 * 1000);
    } catch (InterruptedException e) {
    }
    try {
        //close
    } catch (final IOException e) {
    }
})
.build();
Run Code Online (Sandbox Code Playgroud)

mfu*_*n26 5

来自删除侦听器·缓存说明·google / guava Wiki

警告:删除监听器操作默认情况下是同步执行的,并且由于缓存维护通常是在常规缓存操作期间执行的,因此昂贵的删除监听器会降低正常缓存功能的速度!如果您有昂贵的删除侦听器,请使用RemovalListeners.asynchronous(RemovalListener, Executor)装饰a RemovalListener进行异步操作。

例如

Executor executor = Executors.newFixedThreadPool(10);
CacheBuilder.newBuilder()
        .removalListener(RemovalListeners.asynchronous(notification -> {
            Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MINUTES);
            try {
                //close
            } catch (final IOException e) {
            }
        }, executor))
        .build();
Run Code Online (Sandbox Code Playgroud)