user1745356
user1745356

Reputation: 4683

Are ehcache putAll Operations Thread Safe and Atomic?

The documentation says that ehcache is thread safe. So as far as I understand if thread A updates a cache those updates will always be visible to other threads.

But I wonder if putAll operation is thread safe and atomic? Say, I want to update my cache simply by invoking putAll and passing a Map with actual values. Say, I want to get some value from cache while it is being updated. Will I receive an old value or wait till the cache is updated and receive a new value?

Upvotes: 2

Views: 1336

Answers (1)

Paul Wasilewski
Paul Wasilewski

Reputation: 10372

An operation is atomic when the operation can be only performed entirely or not performed at all. According to Interface Ehcache the putAll operation is not atomic.

This method will throw a NullPointerException if a null element or null key is encountered in the collection, and a partial completion may result (as only some of the elements may have been put).

Regarding if ehcache is threadsafe or not. Ehcache is especially designed to be threadsafe. Take for example the Cache implementation.

Cache is threadsafe.

But beware threadsafe does not mean synchronized. So what's the difference?

Threadsafe means a class can be used from multiple threads without any errors or problems.

Synchronized means one or more methods can be used only from one thread at the same time.

What does it mean in case of putAll and lets say a get call. Both methods are threadsafe but they are not synchronized.

Therefore you can't be sure if Thread A calls putAll and Thread B calls get afterwards that putAll is executed before the get.

Upvotes: 3

Related Questions