Java deadlocks are a bit like people walking across a narrow bridge, occupying one resource while waiting for another, and are a case of multiple tasks occupying code:

public static void main(String[] args) {
    final Object a = new Object();
    final Object b = new Object();
    Thread threadA = new Thread(new Runnable() {
        public void run(a) {
            synchronized (a) {
                try {
                    System.out.println("now i in threadA-locka");
                    Thread.sleep(1000l);
                    synchronized (b) {
                        System.out.println("now i in threadA-lockb"); }}catch (Exception e) {
                    // ignore}}}}); Thread threadB =new Thread(new Runnable() {
        public void run(a) {
            synchronized (b) {
                try {
                    System.out.println("1");
                    Thread.sleep(1000l);
                    synchronized (a) {
                        System.out.println("2"); }}catch (Exception e) {
                    // ignore}}}}); threadA.start(); threadB.start(); }Copy the code

If you want to resolve deadlock related issues:

1 Sequential Execution

2 Multiple object resources are occupied

3 Lock Releases resources