What is the relationship between synchronized and volatile keyword?
When two or more threads access the shared variable and perform read/write operations, this variable should be declared within the synchronized method. The Java language allows threads that access shared variables to keep private working copies of the variables; this allows a more efficient implementation of multiple threads. The volatile modifier requests the Java VM to always access the shared copy of the variable so the its most current value is always read.
Either put the variables in a synchronized method or block, or declare them volatile.