java deadlock demonstration

java deadlock demonstration

java deadlock
Simulated deadlock generation

Deadlock is caused by multiple threads competing for the same resource

package com.feshfans;

/**
 * 1. This code demonstrates deadlock generation in java
 * 2. Deadlock checking method
 */
public class DeadlockShow {

    // Declare two resources
    private static final String ResourceA = "A";
    private static final String ResourceB = "B";

    private static void deadlock(){

        Thread t1= new Thread(new Runnable() {
            @Override
            public void run() {
                // Thread A first acquires ResourceA, that is, thread A holds the lock of ResourceA.
                synchronized (ResourceA){
                    System.out.println("T1 get resource A");
                    try {
                        // Hibernate for 2 seconds to give thread B enough time to get ResourceB
                        // Why use sleep, because the sleep method does not release the lock object
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (ResourceB){
                        System.out.println("T1 get resource B");
                    }

                }

            }
        },"Thread-A");

        Thread t2= new Thread(new Runnable() {
            @Override
            public void run() {
                // Thread B first acquires ResourceB, that is, thread B holds the lock of ResourceB.
                synchronized (ResourceB){
                    System.out.println("T2 get resource B");
                    try {
                        // Hibernate for 1 second to give thread A enough time to get ResourceA
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (ResourceA){
                        System.out.println("T1 get resource A");
                    }
                }

            }
        }, "Thread-B");

        // Start two threads
        t1.start();
        t2.start();
    }

    public static void main(String[] args) {
        deadlock();
        // The main thread does not exit because the deadlock() produces a deadlock and the sub-thread has not been executed.
    }
}
Deadlock view
  1. First use jps to view the pid of the java process, as shown in Figure 1:


View the java process

  1. Use jstack to view java stack information, as shown in Figure 1:

From the figure above, you can clearly see the number of deadlocks, which threads have deadlocks, the memory address and resource type of resources competing between threads (String in this case), the number of lines of code that generate deadlocks, and so on.

Personal suggestion is that when you start a thread, you give each thread a name, so that when problems arise, you will be particularly clear.

Tags: PHP Java Hibernate

Posted on Thu, 10 Oct 2019 09:14:53 -0700 by Alidad