How to write a deadlock
This article introduces you how to write a deadlock, the content is very detailed, interested friends can use for reference, hope to be helpful to you.
Core: cyclic occupancy of locks requested by two (or more) threads
Package com.lau.javabase.lock;import java.util.concurrent.TimeUnit;/** * write a deadlock routine * / public class DeadLockTest {private Object lockA = new Object (); private Object lockB = new Object (); public void printA () throws InterruptedException {synchronized (lockA) {System.out.println ("entered printA..."); TimeUnit.SECONDS.sleep (1) Synchronized (lockB) {System.out.println ("A");}} public void printB () throws InterruptedException {synchronized (lockB) {System.out.println ("entered printB..."); TimeUnit.SECONDS.sleep (1); synchronized (lockA) {System.out.println ("B") } public static void main (String [] args) {DeadLockTest test = new DeadLockTest (); / / A thread new Thread (())-> {try {test.printA ();} catch (InterruptedException e) {e.printStackTrace () }, "A"). Start (); / / B thread new Thread (()-> {try {test.printB ();} catch (InterruptedException e) {e.printStackTrace ();}}, "B") .thread () }} on how to write a deadlock to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.