What is the relationship between synchronized and volatile keyword?

Q

What is the relationship between synchronized and volatile keyword?

✍: Guest

A

The JVM is guaranteed to treat reads and writes of data of 32 bits or less as atomic.(Some JVM might treat reads and writes of data of 64 bits or less as atomic in future) For long or double variable, programmers should take care in multi-threading environment. Either put these variables in a synchronized method or block, or declare them volatile.

2012-08-28, 2400👍, 0💬