Thread chat-java code to achieve a deadlock

When it comes to deadlock, everyone has a common understanding. Deadlock is a kind of waiting for each other, wanting to compete for resources, but unfortunately not giving in, causing the program to fail to run. Thread deadlock always feels better, but when I really let myself explain it, I don’t know what to say from there. Sometimes the interviewer will ask you to write a deadlock. If you write a deadlock, you must understand it, but just write it rashly. It seems that I don’t know where to start. Today we will write a deadlock to make the program crash, haha

1. Use Synchronized implementation

package com.example.demoproject;

public class DeadThread implements Runnable{

    private Object lock1 = new Object() ;

    private Object lock2 = new Object() ;

    public void method() throws Exception{
        System.out.println("死锁开始~~~");
        synchronized (lock1){
            System.out.println(Thread.currentThread().getName()+":锁住了lock1");
            Thread.sleep(1000);
            synchronized (lock2){
                   System.out.println(Thread.currentThread().getName()+":锁住了lock2");
               }
        }
    }

    public void otherMethod() throws Exception{
        System.out.println();
        synchronized (lock2){
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()+":锁住了lock2");
            synchronized (lock1){
                System.out.println(Thread.currentThread().getName()+":锁住了lock1");
            }
        }
    }


    @Override
    public void run() {
        try {
            method();
            otherMethod();
        } catch (Exception e) {

        }
    }

    public static void main(String[] args) {
        DeadThread deadThread = new DeadThread();
        new Thread(deadThread,"线程A").start();
        new Thread(deadThread,"线程B").start();

    }
}

The method method locks lock1 and then lock2. Of course, it waits in the middle and waits for otherMethod to lock lock2. Otherwise, the method is executed and the otherMethod locks again and no deadlock occurs. The sleep wait here is to simulate the situation that may occur when the thread preempts resources in the case of high concurrency.

Take a closer look. In fact, this is not very good. Method() and otherMethod() are started with a thread respectively, and deadlock can also occur. In order to write one less Thread or one less Runnable, directly Putting them under a run method is actually a bit redundant. Below we directly use the most trouble-free way to write a deadlock. Of course, the code saves trouble, not the lock saves trouble. Realizing a deadlock is definitely the most convenient way to synchronize, without having to unlock the operation.

2. Use ReentrantLock to lock

public class DeadThreadLock {

    private static Lock lock1 = new ReentrantLock() ;

    private static Lock lock2 = new ReentrantLock() ;

    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock1.lock();
                    System.out.println(Thread.currentThread().getName()+"lock1加锁成功");
                    Thread.sleep(1000);
                    lock2.lock();
                    System.out.println(Thread.currentThread().getName()+"lock1,2加锁成功");
                    lock1.unlock();
                    lock2.unlock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"线程A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock2.lock();
                    System.out.println(Thread.currentThread().getName()+"lock2加锁成功");
                    Thread.sleep(1000);
                    lock1.lock();
                    System.out.println(Thread.currentThread().getName()+"lock1,2加锁成功");
                    lock1.unlock();
                    lock2.unlock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"线程B").start();
    }

}

The code is very plain, that is, two locks compete, thread A occupies lock1, thread B occupies lock2, and then thread B waits for thread A to release lock1 in the blocking queue of lock1, and thread A waits for thread B to release lock2 in the blocking queue of lock2 , I want to pierce my eyes, but unfortunately, I'm dead, oops, I'm stuck, I'm always embarrassed, haha. If you are interested, you can write it down and try it yourself. There are many ways to solve the deadlock. Is it not easy to create a deadlock? Haha, we are all people who can create problems! !

No sacrifice,no victory~

Guess you like

Origin blog.csdn.net/zsah2011/article/details/109138522