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