Java concurrency, synchronous lock performance testing

Source: Internet
Author: User

The test is mainly from the running time difference, the larger the amount of data, the more obvious the time difference, examples are as follows:

1 Package com.xt.thinks21_2;2 3 /**4 * Synchronous lock Performance Test5  * 6 * @author Administrator7  *8  */9  Public classSynchronizedtimetest {Ten      Public volatile intinc =0; One  A      Public voidIncrease () { -Inc++; -     } the  -      Public Static voidMain (string[] args) { -Final Synchronizedtimetest test =Newsynchronizedtimetest (); -          for(inti =0; I <Ten; i++) { +             NewThread () { -                  Public voidrun () { +                      for(intj =0; J <10000; J + +) A test.increase (); at                 }; - }.start (); -         } -Long time1 =System.currenttimemillis (); -          while(Thread.activecount () >1) { -             //ensure that the previous threads are finished executing inThread.yield(); -         } toLong time2 =System.currenttimemillis (); +System. out. println ("time1:"+ time1 +"time2:"+time2); -Long Timediff = time2-time1; theSystem. out. println ("Time :"+ Timediff +" -"+test.inc); *     } $}

Above is the method without synchronized running result:

time1:1429805281187 time2:1429805281187
time:0-->75809

method to add the synchronized run result:

time1:1429805416628 time2:1429805416645
time:17-->100000

Java concurrency, synchronous lock performance testing

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.