Volatile means that this variable does not have to be optimized with local replicas to ensure that all threads directly manipulate the variables in main memory and are truly shared.
Volatile is about visibility, independent of atomic operations and thread safety.
Synchronized is often emphasized as a mutex, guaranteeing that only one thread enters. In fact, it also has the meaning of ensuring visibility, that is, the incoming thread will certainly see the previous entry of the thread's update results. If there is no synchronized, even if the variable being assigned is just an int, is an atomic operation (that is, the second thread either reads the value before the update, or reads the value after the update, does not read an intermediate state), and there is a problem with the visibility, that is, after the thread may read or the old value, There is a problem with the application logic. Why allow this, because since you do not say synchronization, the threads are completely independent of each other.
So synchronized has two layers of meaning.
1 The exclusive during the execution of the code block to ensure that the other threads do not disturb, so as to ensure the integrity of the data,
2) After the execution of the visible. Ensure that the threads that enter after the thread can see the results of the line.
Visibility, atomicity is two different problems.