What happens if a volatile variable is written from 2 threads?

It's because read-modify-write operations on volatile variables are not atomic. v++ is actually something like:

r1 = v;
r2 = r1 + 1;
v = r2;

So if you have two threads performing this operation once each, it could possibly result in the variable being incremented only once, as they both read the old value. That's an example of why it's not safe.

In your example it would be not safe if you removed synchronized, made the field volatile and had two threads calling setValue after some conditional logic based on the return of getValue - the value could have been modified by the other thread.

If you want atomic operations look at the java.util.concurrent.atomic package.


If you write volatile variable from multiple threads without using any synchronized constructs, you are bound to get data inconsistency errors.

Use volatile variables without synchronization in case of single write thread and multiple read threads for atomic operations.

Volatile make sure that variable value is fetched from main memory instead of Thread cache. It's safe to use in case of single write and multiple read operations.

Use Atomic variables or synchronization or Lock API to update and read variables from multiple threads.

Refer to related SE question:

What is meant by "thread-safe" code?


The keyword volatile is used to ensure that changes to your Object will be seen by other Threads. This does not enforce, that non-atomic operations on the Object will be performed without an other Thread interfering before the operation is finished. For enforcing this you will need the keyword synchronized.