For synchronized, to simulate deadlocks, there are certainly many examples on the Internet, and they are relatively implemented. But how can the Lock class deadlock? How to use it? Fewer examples have been found. And a lot of code, oh, looks ugly and messy. I tried to achieve it by myself, and at one time I was confused when I wrote it. It was quite simple that I realized it by coincidence.
package com.mytest.code_right.sys_lock; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Test6 { public static void main(String args[]) { // thread deadlock final A a = new A(); final B b = new B(); // Thread A new Thread(new Runnable() { public void run() { System.out.println("thread A Start execution"); a.a3(b); } }, "thread A").start(); // Thread B new Thread(new Runnable() { public void run() { System.out.println("thread B Start execution"); b.b3(a); } }, "thread B").start(); } // Resource A static class A { private final Lock lock = new ReentrantLock(); public void a2() { System.out.println("Unlocked entry a.a2()" + Thread.currentThread().getName()); lock.lock(); System.out.println("Get into a.a2()"); lock.unlock(); } public void a3(B b) { System.out.println("Get into a3 Start lock" + Thread.currentThread().getName()); lock.lock(); System.out.println("Get into a3 lock" + Thread.currentThread().getName()); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Get into a3 lock>>Start execution b.b2" + Thread.currentThread().getName()); b.b2(); lock.unlock(); } } // Resource B static class B { private final Lock lock = new ReentrantLock(); public void b2() { System.out.println("Unlocked entry b.b2()" + Thread.currentThread().getName()); lock.lock(); System.out.println("Get into b.b2"); lock.unlock(); } public void b3(A a) { System.out.println("Get into b3 Start lock" + Thread.currentThread().getName()); lock.lock(); System.out.println("Get into b3 lock" + Thread.currentThread().getName()); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Get into b3 lock>>Start execution a.a2" + Thread.currentThread().getName()); a.a2(); lock.unlock(); } } }
In fact, the above examples are for reference.
Writing High Quality Code: 151 Suggestions for Improving Java Programs - Qin Wavelet
Here are some examples of Lock and synchronized.
summary
In fact, when I wrote it, I found that I didn't understand Lock properly. I read the code analysis before I understood the meaning of the book.
This is because for synchronous resources (code blocks in the example), explicit locks are object-level locks, while internal locks are class-level locks, that is, Lock locks are object-following, synchronized locks are class-following, and more simply, defining Lock as a private property of multithreaded classes is not mutually exclusive unless Lock is defined as a shared variable for all threads.
That is to say, Lock is an object-level lock that locks the object.
### Code examples The reason for the deadlock in the above example is as follows: Thread B locks class B and then wants to get the method of class A to lock and execute, so it needs to lock object A, thread A locks class A, and method of class B to lock and execute, so it needs to lock object B. So they wait for each other, deadlock. If the thread.sleep time of the above code is removed and executed several times, it will be found that sometimes there will be no thread deadlock. This means that thread A will finish executing first and then go to thread B to execute without waiting.