A timer, open 100 threads at the same time, each thread sleeps 1ms, after the global static variable count plus 1, after the 100 threads are created, Hibernate 500ms, the total time to calculate, the program is as follows:
Public classCounter { Public volatile Static intCount = 0; Public Static voidInc () {//The delay is 1 milliseconds, making the result obvious Try{Thread.Sleep (1); } Catch(Interruptedexception e) {} count++; } Public Static voidMain (string[] args) {LongTime1 =System.currenttimemillis (); System.out.println (); for(inti = 0; I < 100; i++) { NewThread (NewRunnable () {@Override Public voidrun () {counter.inc (); }}). Start (); } Try{Thread.Sleep (500); } Catch(interruptedexception e) {e.printstacktrace (); } //each run may have a different value, possiblySystem.out.println ("counter.count=" +counter.count); LongTime2 =System.currenttimemillis (); System.out.println ("Time Consuming:" + (time2-time1)); }}
Operation Result:
counter.count=96
Time: 506
Counter.count is not equal to 100 because the count++ thread is unsafe.
Plus the synchronization code block:
public class Counter {public volatile static int count = 0;static Object obj = new Object ();p ublic Static Void Inc () {//This A delay of 1 milliseconds, making the result obvious try {thread.sleep (1);} catch (Interruptedexception e) {}synchronized (obj) {count++;}} public static void Main (string[] args) {Long time1 = System.currenttimemillis (); System.out.println (); for (int i = 0; i <; i++) {New Thread (new Runnable () {@Overridepublic void run () {Counter.inc ( );}}). Start ();} try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} There may be different values for each run, possibly 1000system.out.println ("counter.count=" + counter.count), long time2 = System.currenttimemillis ( ); System.out.println ("Time-consuming:" + (TIME2-TIME1));}}
Operation Result:
counter.count=100
Time: 507
So synchronized to the speed of the operation of how much impact, so I 100,1000,10000,100000 with 500,000 times, will not sync, synchronized (obj) and synchronized ( Counter.class) results were compared as follows:
| Number of threads |
Not syncing < testing more Data > |
Synchronized (obj) |
Synchronized (Counter.class) |
| 100 |
counter.count=97 Time: 508 <98-506><98-507><97-507><100-508> |
counter.count=100 Time: 507 <100-507><100-507><100-508><100-508> |
counter.count=100 Time: 506 <100-508><100-508><100-506><100-509> |
| 1000 |
counter.count=997 Time: 572 <993-562><994-562><995-561><990-561> |
counter.count=1000 Time: 562 <1000-572><1000-560><1000-559><1000-560> |
counter.count=1000 Time: 570 <1000-585><1000-560><1000-562><1000-561> |
| 10000 |
counter.count=9947 Time: 1067 <9951-1077><9962-1079><9936-1066><9953-1081> |
counter.count=10000 Time: 1074 <10000-1075><10000-1068><10000-1079><10000-1074> |
counter.count=10000 Time: 1072 <10000-1089><10000-1076><10000-1108><10000-1076> |
| 100000 |
counter.count=99399 Time-consuming: 6098 <99347-6111><99374-6099><99416-6196><99459-6137> |
counter.count=100000 Time: 6103 <100000-6101><100000-6104><100000-6155><100000-6107 |
counter.count=100000 Time: 6150 <100000-6287><100000-6182 ><100000-6159><100000-6104> |
| 500000 |
counter.count=497242 Time: 28892 <497394-30674><497274-29864><497129-29470><497216-29529> |
counter.count=500000 Time: 31232 <500000-29297><500000-29053><500000-29166><500000-29006> |
counter.count=500000 Time: 29251 <500000-29743><500000-29334><500000-29600><500000-29848> |
Averaging the time taken above, you get:
| Number of threads |
Average elapsed time without synchronization |
Synchronized (obj) Average time |
Synchronized (Counter.class) Average time |
| 100 |
507.2 |
507.4 |
507.4 |
| 1000 |
563.6 |
562.6 |
567.6 |
| 10000 |
1074 |
1074 |
1084.2 |
| 100000 |
6128.2 |
6114 |
6176.4 |
| 500000 |
29685.8 |
29550.8 |
29555.2 |
Thus, the conclusion is:
1, synchronized can solve the problem of count++ inaccuracy caused by multithreading.
2, with the increase of the number of threads, add the code after synchronized, and will not have a significant impact on speed.
3, from 10,000 and 100,000 of the results, after synchronized (obj) processing procedures, than synchronized (Counter.class) after the process of processing about 10ms faster. With 100, 1000, and 500,000, synchronized (obj) and synchronized (Counter.class) perform almost the same speed. Overall, as the number of threads increases, theefficiency of synchronized (Counter.class) execution is not significantly lower than synchronized (obj).
count++ thread safety and synchronized performance impact testing