Rama Vadakattu
Rama Vadakattu

Reputation: 1266

ConcurrentHashMap.clear() What will happen to read threads?

We were maintaining the cache using ConcurrentHashMap Often times the cache needs to be refreshed What is the bestway to refresh the cache?

1.Update the cache , Remove all stale keys.

2.Clear the cache and insert fresh keys again from the content.

Case 1: all read threads will not be blocked, once updated they will get the updated value.

Case 2: Here my question what will happen to read threads when we will clear the cache?Do they get null value ?

What is the best strategy to update the cache?

Upvotes: 3

Views: 1970

Answers (1)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136062

Most reliable answer as usual is in API, which for this situation says ... For aggregate operations such as putAll and clear, concurrent retrievals may reflect insertion or removal of only some entries...

Upvotes: 2

Related Questions