Run the followingCode:
Package COM. test. WWW; public class test {public static int COUNT = 0; public static void Inc () {// The delay is 1 millisecond, making the result obviously try {thread. sleep (1);} catch (interruptedexception e) {} count ++;} public static void main (string [] ARGs) {// start 1000 threads at the same time, perform I ++ calculation and check the actual result for (INT I = 0; I <1000; I ++) {New thread (New runnable () {@ override public void run () {test. INC (); system. out. println (thread. currentthread (). getname ());}}). start () ;}try {thread. sleep (3000);} catch (exception e) {}// the value of each run may be different, which may be 1000 system. out. println ("Run result: counter. count = "+ test. count );}}
The final running results often change, sometimes 1000 or sometimes 999.
Added the volidate modifier to the Count field to ensure that the data obtained from the main thread is the latest each time. But the result is still like this? Why?
Public class test {public volatile static int COUNT = 0; public static void Inc () {// The delay is 1 millisecond, making the result obviously try {thread. sleep (1);} catch (interruptedexception e) {} count ++;} public static void main (string [] ARGs) {// start 1000 threads at the same time, perform I ++ calculation and check the actual result for (INT I = 0; I <1000; I ++) {New thread (New runnable () {@ override public void run () {test. INC (); system. out. println (thread. currentthread (). getname ());}}). start () ;}try {thread. sleep (3000);} catch (exception e) {}// the value of each run may be different, which may be 1000 system. out. println ("Run result: counter. count = "+ test. count );}}
The Java garbage collection article describes the memory allocation during JVM runtime. One memory area is the JVM Virtual Machine stack, and each thread has a thread stack when running,
The thread stack stores the variable value information during thread running.When the thread accesses a value of an object, it first finds the value of the variable corresponding to the heap memory through the object reference, and then sets the heap memory
The specific value of the variable is loaded into the local memory of the thread and a copy of the variable is created. Then, the thread no longer has any relationship with the value of the variable in the heap memory, but directly modifies the value of the copy variable,
At a time point after modification (before the thread exits), the value of the copy of the thread variable is automatically written back to the object variable in the heap. In this way, the value of the object in the heap changes.The following figure
Read and load: copy the variable from the primary memory to the current working memory.
Use and assign run the code to change the value of the shared variable.
Store and write
Here, use and assign can appear multiple times
However, these operations are not atomic, that is, after the read load, if the Count variable in the main memory is modified, the value in the thread's working memory will not change because it has been loaded, therefore, the calculated results are different from the expected ones.
For variables modified by volatile, the JVM Virtual Machine only ensures that the value loaded from the primary memory to the thread's working memory is up to date.
For example, if thread 1 and thread 2 are performing the read and load operations and find that the Count value in the main memory is 5, the latest value will be loaded.
After the thread 1 heap count is modified, it will be written to the main memory, and the count variable in the main memory will change to 6
Thread 2 because the read and load operations have been performed, after the operation, the variable value of the main memory count will also be updated to 6
As a result, concurrency still exists after the two threads are modified with the volatile keyword in a timely manner.