Synchronized
Action on object instance: Lock the given object and get the lock of the given object before entering the synchronization code.
Acting on the instance method: it is equivalent to lock the current instance and get the lock of the current instance before entering the synchronization code.
Acting on a static method: the equivalent of locking the current class, before entering the synchronization code to obtain the current class lock.
Use to lock an instance object
Public classAccountingsyncImplementsRunnable {StaticAccountingsync instance =New Accountingsync();Static inti =0;@Override Public void Run() { for(intK =0; K <10000; k++) {synchronized(instance) {i++; } } }@Test Public void Testinteger()throwsinterruptedexception {intCount =Ten; thread[] ts =NewThread[count]; for(intK =0; K < count; k++) {Ts[k] =NewThread (instance); }//Start for(intK =0; K < count; k++) {Ts[k].Start(); }//Join for(intK =0; K < count; k++) {Ts[k].Join(); } System. out.println(i); }}
Lock A class method
Public classAccountingSync2ImplementsRunnable {StaticACCOUNTINGSYNC2 instance =New ACCOUNTINGSYNC2();Static inti =0; Public synchronized void Increase() {i++; }@Override Public void Run() { for(intK =0; K <10000; k++) {Increase(); } }@Test Public void Testinteger()throwsinterruptedexception {intCount =Ten; thread[] ts =NewThread[count]; for(intK =0; K < count; k++) {Ts[k] =NewThread (instance); }//Start for(intK =0; K < count; k++) {Ts[k].Start(); }//Join for(intK =0; K < count; k++) {Ts[k].Join(); } System. out.println(i); }}
Error demo for locking class method
public class AccountingSyncBad implements Runnable { static int i = 0; public synchronized void increase() { i++; } @Override public void run() { for (int k = 0; k < 10000; k++) { increase(); } } @Test public void testInteger() throws InterruptedException { int count = 10; Thread[] ts = new Thread[count]; for (int k = 0; k < count; k++) { ts[k] = new Thread(new AccountingSyncBad()); } // start for (int k = 0; k < count; k++) { ts[k].start(); } // join for (int k = 0; k < count; k++) { ts[k].join(); } System.out.println(i); }}
Suppose you compare each instance of an instance lock with a door, and each door in the test method has a lock but 10 doors and 10 locks, each of which enters a door. There is still no guarantee that the critical zone resource I can access only one thread at a time
Fix
@Test Public void Testintegerfix()throwsinterruptedexception {intCount =Ten; Accountingsyncbad instance =New Accountingsyncbad(); thread[] ts =NewThread[count]; for(intK =0; K < count; k++) {Ts[k] =NewThread (instance); }//Start for(intK =0; K < count; k++) {Ts[k].Start(); }//Join for(intK =0; K < count; k++) {Ts[k].Join(); } System. out.println(i);}
To lock a static class method
public class Accountingsyncclass implements Runnable {static int i = 0; public static synchronized void increase () {i++; } @Override public void Run () {for (int k = 0; k < 10000; k++) {increase (); }} @Test public void Testinteger () throws interruptedexception {int count = 10; thread[] ts = new Thread[count]; for (int k = 0; k < count; k++) {ts[k] = new Thread (new Accountingsyncclass ()); }//start for (int k = 0; k < count; k++) {Ts[k].start (); }//join for (int k = 0; k < count; k++) {ts[k].join (); } System.out.println (i); } @Test public void Testintegerfix () throws interruptedexception {int count = 10; Accountingsyncclass instance = new Accountingsyncclass (); thread[] ts = new Thread[count]; for (int k = 0; k < count; k++) {ts[k] = new Thread (instance); }//start for (int k = 0; k < count; k++) {Ts[k].start (); }//join for (int k = 0; k < count; k++) {ts[k].join (); } System.out.println (i); }}
Both the Testinteger method and the Testintegerfix method above are able to get the correct result, because the static class method locks the same lock equivalent to 10 doors, guaranteeing that only one thread can access the critical zone resource I at the same time.
Java Thread Synchronization Operations