Detailed explanation of thread synchronization synchronized and volatile of Java threads

In the previous part, a simple example was given to illustrate thread safety and insecurity. In the case of insecurity in the example, the output results are just incremented one by one (in fact, it is a coincidence that running multiple times will produce different output results). Why does this result occur? Because the established count object is shared by threads, a thread changes the value of its member variable num, The next thread happens to read the modified num, so it will increment the output.

To illustrate the thread synchronization problem, we must first explain the two characteristics of Java threads, visibility and ordering. Multiple threads cannot directly transfer data interaction, and their interaction can only be realized by sharing variables. Take the example in the previous blog post to illustrate that multiple threads share an object of count class. This object is created in main memory (heap memory). Each thread has its own working memory (thread stack). The working memory stores a copy of the main memory count object. When a thread operates the count object, it first copies the count object from main memory to working memory, Then execute the code count Count(), changes the num value, and finally refreshes the main memory count with the working memory count. When an object has copies in multiple memories, if a shared variable is modified in one memory, other threads should also be able to see the modified value. This is visibility. When multiple threads are executed, the CPU's scheduling of threads is random. We don't know which step the current program is executed and then switch to the next thread. A classic example is the bank remittance problem. When a bank account deposits 100, one person withdraws 10 yuan from the account and another person remits 10 yuan to the account, the balance should still be 100. This may happen at this time. Thread a is responsible for withdrawal and thread B is responsible for remittance. A reads 100 from the main memory and B reads 100 from the main memory. A performs the minus 10 operation and flushes the data to the main memory. At this time, the main memory data 100-10 = 90, while memory B performs the plus 10 operation and flushes the data to the main memory. Finally, the main memory data 100 + 10 = 110, Obviously, this is a serious problem. We should ensure that thread a and thread B execute in an orderly manner. Withdrawal before remittance or remittance before withdrawal. This is order. This article describes jdk5 0. For more advanced synchronization methods, please refer to the lock object lock synchronization of Java threads for a more perfect processing method code example.

The above is all about the detailed explanation of thread synchronization synchronized and volatile of Java threads in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this site: Java multithreaded programming example, creating and running a java thread method introduction, detailed explanation of thread safety of various sets of Java, etc. if you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>