volatile keyword Learning record 1

Source: Internet
Author: User
Tags volatile

Although has been working for six months ... Although it has been doing web development .... However, there is always little use of multithreading .....

And I've been reading about it lately. So there's this article.

Use examples to illustrate the problem.

1  Public classVolatiletest {2     Booleanb =false;3     intA = 0;4 5      Public Static voidMain (string[] args) {6          for(inti=0; i<1000000; i++) {7Volatiletest resource =Newvolatiletest ();8Thread T1 =NewThread (NewA (Resource));9Thread t2 =NewThread (NewB (Resource));Ten T1.start (); One T2.start (); A             //System.out.println (i); -         } -     } the } -  - classAImplementsRunnable { -Volatiletest r =NULL; +  -      PublicA (Volatiletest a) { +R =A; A     } at  -      Public voidrun () { -R.A = 1; -R.b =true; -     } -  in } -  to classBImplementsRunnable { +Volatiletest r =NULL; -  the      PublicB (Volatiletest a) { *R =A; $     }Panax Notoginseng  -      Public voidrun () { the          while(!r.b) { + Thread.yield (); A         } the         inttemp =R.A; +         if(temp = = 0) { -SYSTEM.OUT.PRINTLN ("CPU command reflow"); $         } $     } -}

In this example, I've output a total of 11 CPU command reflow

The reason for this output is that although the code in Class A is set to R.A to 1, then modify R.b to True. (line:25,26) But it's still possible to do the 26th line of code when the CPU executes, and then do 25 lines of code. Because there is no correlation between the 2 lines of code. Therefore, the optimization may change the order. This results in the Output CPU command reflow

This is not the case if you add the volatile keyword to the B property of Volatiletest. It guarantees that the order of the objects in front of this object does not swap with the sequence of subsequent operations. Equivalent to dividing the original code into 2 segments (a class of the Run method before 25 lines of code is a paragraph, 25 lines after the code is a paragraph, just here run in the code is relatively small, and 2 paragraphs before and after the code order will not be reversed, but the 2 code of their own internal order can be reversed.

This problem does not occur in a single thread, because if there is an association between the 2 lines of code, then the CPU will ensure that the previous code executes before the last sentence. But not guaranteed in multithreading. That's why you need the volatile keyword.

My understanding of volatile now is that it is like a weakened version of synchronized. It can block concurrency problems in some cases, but other cases of concurrency problems are not organized.

volatile keyword Learning record 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.