Java thread safety experiments on atomicity and volatile

Source: Internet
Author: User

1. Variable Increment test
1     Static /*volatile*/ intShared=0;//volatile also cannot guarantee the atomicity of the + + operation2     Static synchronized intIncrshared () {//shared end result value is less than expected if synchronized is not added3         return++Gkfx;4     }5      Public Static voidTestincrshared (string[] args)throwsinterruptedexception {6Shared=0;7thread[] Thrds =NewThread[20];8          for(intj=0;j<thrds.length;j++){9THRDS[J] =NewThread (NewRunnable () {Ten @Override One                  Public voidrun () { A                      for(intk=0;k<1000;k++){ - System.out.println (incrshared ()); -                     } the                 } -             }); -         } -          for(intj=0;j<thrds.length;j++){ + Thrds[j].start (); -         } +          for(intj=0;j<thrds.length;j++){ A Thrds[j].join (); at         } - System.out.println (shared); -}
2. Volatile test
1     Static /*volatile*/ intA0,A1;//If you add volatile here, you can avoid r0==r1==0 results.2     Static /*volatile*/ intR0,R1;//There is no way to avoid r0==r1==0 results even with volatile3      Public Static voidTestvolatile (string[] args)throwsinterruptedexception {4         int[] A=New int[2];5         int[] r=New int[2];6         Final intsleep=10;7         FinalObject lock=NewObject ();8Runnable run1=NewRunnable () {9 @OverrideTen              Public voidrun () { One                 Try { A Thread.Sleep (sleep); -}Catch(interruptedexception e) { - e.printstacktrace (); the                 } -                 //synchronized (lock) {//locking can also establish happens-before relationships to avoid r0==r1==0 results. -A0=1; -                 //} +r1=A1; -             } +         }; ARunnable run2=NewRunnable () { at @Override -              Public voidrun () { -                 Try { - Thread.Sleep (sleep); -}Catch(interruptedexception e) { - e.printstacktrace (); in                 } -                 //synchronized (lock) { toA1=1; +                 //} -r0=A0; the             } *         }; $ Thread thrd1;Panax Notoginseng Thread Thrd2; -          the         inti; +         int[] acnt=New int[2] [2]; A         int[] rcnt=New int[2] [2]; the          for(i=0;i<10000;i++){ +A0=a1=0; -R0=r1=0; $Thrd1 =NewThread (RUN1); $Thrd2 =NewThread (run2); -              - Thrd1.start (); the Thrd2.start (); -             Wuyi Thrd1.join (); the Thrd2.join (); -              Wua[0]=A0; -a[1]=A1; Aboutr[0]=R0; $r[1]=R1; - System.out.println (i); - System.out.println (Arrays.tostring (a)); - System.out.println (arrays.tostring (R)); Aacnt[a[0]][a[1]]++; +rcnt[r[0]][r[1]]++; the         } - System.out.println (arrays.deeptostring (acnt)); $ System.out.println (arrays.deeptostring (rcnt)); the}

3. Volatile Test 2
1     Static Booleanshouldstop=false;2      Public Static voidTestVolatile2 (string[] args)throwsinterruptedexception {3shouldstop=false;4Runnable run1=NewRunnable () {5 @Override6              Public voidrun () {7                 intI=0;8                  while(!shouldstop) {//cannot read to thread 2 modified Shouldstop value, resulting in an infinite loop9i++;Ten                     //System.out.println (i);//If you call another function, you can read the latest value of Shouldstop . One                 } A             } -         }; -Runnable run2=NewRunnable () { the @Override -              Public voidrun () { -                 Try { -Thread.Sleep (10); +}Catch(interruptedexception e) { - e.printstacktrace (); +                 } Ashouldstop=true; at             } -         }; - Thread thrd1; - Thread Thrd2; -          -         inti; in         int[] acnt=New int[2] [2]; -         int[] rcnt=New int[2] [2]; to          for(i=0;i<100;i++){ +Thrd1 =NewThread (RUN1); -Thrd2 =NewThread (run2); the              * Thrd1.start (); $ Thrd2.start ();Panax Notoginseng  - Thrd2.join (); the System.out.println (i); + System.out.println (shouldstop); A Thrd1.join (); the              +         } -}

Java thread safety experiments on atomicity and volatile

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.