0

Since Java 8 we can use .compute* methods on ConcurrentHashMap to synchronize processing by key, so that if two threads execute .compute* method on the same key at the same time, callbacks still will be executed one after another and not simultaneously. But ConcurrentHashMap doesn't provide ability to delete data in timely fashion as caches usually allow.

Guava/Caffeine caches provide ability to automatically delete values based on time, but you don't have that nasty feature of synchronized processing based on key, as in ConcurrentHashMap (you can get ConcurrentMap using asMap method, but .compute* implementations doesn't provide synchronization based on key)

My goal is to have both processing synchronization by key as in ConcurrentHashMap AND removals by time as in Guava/Caffeine.

What is the best way to achieve it in Java?

4

1 回答 1

0

I was wrong about Caffeine - it supports atomic operations with compute. Guava support was added since 21 version.

于 2019-09-06T15:49:05.113 回答