DEVFYI - Developer Resource - FYI

What is the relationship between synchronized and volatile keyword?

Java Interview Questions and Answers (part 1)


(Continued from previous question...)

142. What is the relationship between synchronized and volatile keyword?

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.

(Continued on next question...)

Other Interview Questions