1. Atomicity
In addition to atomic JVM itself provides the basic data types of read and write operations, the can synchronized
and Lock
atomicity. Because synchronized
and Lock
can guarantee that only one thread accesses the code block at any time.
2. Visibility issues
volatile
Keyword guarantees visibility. When a shared variable is modified by volatile, it will ensure that the modified value is immediately seen by other threads, that is, the modified value is immediately updated to the main memory. When other threads need to read it, it will go to the memory to read the new value.
synchronized
And Lock
can also guarantee visibility, because they can guarantee that only one thread can access the shared resource at any time, and flush the modified variables to memory before it releases the lock.
3. Problem of order
Orderliness can be guaranteed through volatile
Orderliness can be ensured through synchronized
· and· Lock·
. Obviously, synchronized and Lock ensure that one thread executes synchronization code at each moment, which is equivalent to allowing threads to execute synchronization code sequentially, which naturally ensures orderliness.
The most complete concurrent programming mind map in history:https://www.processon.com/view/5b1f1ad7e4b03f9d251c06e5#map