1. Synchronized added to common methods

Public synchronized void method1() {system.out.println (thread.currentThread ().getName() + "1"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }} public synchronized void method2() {system.out.println (thread.currentThread ().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }}Copy the code

1.1 Call through the same object reference

SynchronizedDemo demo1 = new SynchronizedDemo();
Thread t1 = new Thread(() -> demo1.method1());
Thread t2 = new Thread(() -> demo1.method2());
t1.start();
t2.start();
Copy the code

Test result: blocks because synchronized adds a reference to the current class to a method

1.2 Call through different object references

SynchronizedDemo demo1 = new SynchronizedDemo();
SynchronizedDemo demo2 = new SynchronizedDemo();
Thread t1 = new Thread(() -> demo1.method1());
Thread t2 = new Thread(() -> demo2.method2());
t1.start();
t2.start();
Copy the code

Test result: No blocking, because the reference object of the current class is locked and the call uses a different object

2. Synchronized added to static methods

Public static synchronized void method1() {system.out.println (thread.currentThread ().getName() + ""); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }} public static synchronized void method2() {system.out.println (thread.currentThread ().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }}Copy the code

2.1 Call from the same object

SynchronizedDemo demo1 = new SynchronizedDemo();
Thread t1 = new Thread(() -> demo1.method1());
Thread t2 = new Thread(() -> demo1.method2());
t1.start();
t2.start();
Copy the code

Test result: block because the method is static and the Class has only one copy

2.2 Call through different objects

SynchronizedDemo demo1 = new SynchronizedDemo();
SynchronizedDemo demo2 = new SynchronizedDemo();
Thread t1 = new Thread(() -> demo1.method1());
Thread t2 = new Thread(() -> demo2.method2());
t1.start();
t2.start();
Copy the code

Test result: blocked, for the same reason as above

3. Synchronize code block to lock this

Public void method3() {synchronized (this) {system.out.println (thread.currentThread ().getName() + "access to this "); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public void method4() { synchronized (this) { System.out.println(Thread.currentThread().getName() + "Access this sync block 4"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }}}Copy the code

3.1 Call through the same object

SynchronizedDemo demo1 = new SynchronizedDemo();
Thread t1 = new Thread(() -> demo1.method3());
Thread t2 = new Thread(() -> demo1.method4());
t1.start();
t2.start();
Copy the code

Test result: blocks because this is an instance of the object and therefore locks the same object

3.2 Call through different objects

SynchronizedDemo demo1 = new SynchronizedDemo();
SynchronizedDemo demo2 = new SynchronizedDemo();
Thread t1 = new Thread(() -> demo1.method3());
Thread t2 = new Thread(() -> demo2.method4());
t1.start();
t2.start();
Copy the code

Test result: No blocking because two different object instances are used

Summary: Synchronized applies to normal methods in the same way that this does: it locks the current object instance

4. Synchronize code blocks to lock different objects

Object lock1 = new Object(); Object lock2 = new Object(); Public void method5() {synchronized (lock1) {system.out.println (thread.currentThread ().getName() + "access to lock1 "); synchronized (lock1) {system.out.println (thread.currentThread ().getName() +" access to lock1 ");  try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public void method6(){ synchronized (lock2) { System.out.println(Thread.currentThread().getName() + "Access synchronization block 6 of Lock2 "); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }}}Copy the code

4.1 Call from the same object

SynchronizedDemo demo1 = new SynchronizedDemo();
Thread t1 = new Thread(() -> demo1.method5());
Thread t2 = new Thread(() -> demo1.method6());
t1.start();
t2.start();
Copy the code

Test result: No blocking because different object instances under the same reference are locked

5. Block synchronization locks the same Object

Public void method5() {synchronized (lock1) {system.out.println (thread.currentThread ().getName() + "access to lock1 "); synchronized (lock1) {system.out.println (thread.currentThread ().getName() +" access to lock1 ");  try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public void method6(){ synchronized (lock1) { System.out.println(Thread.currentThread().getName() + "Access synchronization block 6 of Lock1 "); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }}}Copy the code

5.1 Using the Same Object to call

SynchronizedDemo demo1 = new SynchronizedDemo();
Thread t1 = new Thread(() -> demo1.method5());
Thread t2 = new Thread(() -> demo1.method6());
t1.start();
t2.start();
Copy the code

Test result: blocks because it locks the same object instance under the same reference. It does not block if different objects are locked or invoked using different object instances

6. Synchronized code blocks lock class objects

public void method7() { synchronized (SynchronizedDemo.class) { System.out.println(Thread.currentThread().getName() + "Access Class synchronized block 5"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public void method8(){ synchronized (SynchronizedDemo.class) { System.out.println(Thread.currentThread().getName() + "access Class block 6"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }}}Copy the code

6.1 Using Different Objects to call

SynchronizedDemo demo1 = new SynchronizedDemo();
SynchronizedDemo demo2 = new SynchronizedDemo();
Thread t1 = new Thread(() -> demo1.method7());
Thread t2 = new Thread(() -> demo2.method8());
t1.start();
t2.start();
Copy the code

Class = Xxx; class = Xxx; class = Xxx; class = Xxx;