[Big Data] Study Notes 1 Java SE Chapter 9 Multithreading 9.7 Release Lock Operation and Deadlock

[Big Data] Study Notes

insert image description here

1 Java SE

Chapter 9 Multithreading

9.7 Release lock operation and deadlock

Before any thread enters a synchronization code block or a synchronization method, it must first obtain a lock on the synchronization monitor, so when will the lock on the synchronization monitor be released?

【1】The operation of releasing the lock

The execution of the synchronization method and synchronization code block of the current thread ends.

An unhandled Error or Exception occurs in the synchronization code block or synchronization method of the current thread, causing the current thread to end abnormally.

The current thread executes the wait() method of the lock object in the synchronization code block and synchronization method, the current thread is suspended, and the lock is released.

[2] Operations that do not release the lock

When a thread executes a synchronization code block or a synchronization method, the program calls the Thread.sleep() and Thread.yield() methods to suspend the execution of the current thread.

When a thread executes a synchronization code block, other threads call the suspend() method of the thread to suspend the thread, and the thread will not release the lock (synchronization monitor). Obsolescence such as suspend() and resume() to control threads should be avoided as much as possible.

[3] deadlock

Different threads lock the synchronization monitor object required by the other party and do not release it. When they are waiting for the other party to give up first, a thread deadlock is formed. Once a deadlock occurs, the entire program will neither be abnormal nor give any prompts, but all threads are blocked and cannot continue.

package com.dingjiaxiong;

/**
 * @Projectname: BigDataStudy
 * @Classname: TestDeadLock
 * @Author: Ding Jiaxiong
 * @Date:2023/4/27 16:20
 */

public class TestDeadLock {
    
    
    public static void main(String[] args) {
    
    
        Object g = new Object();
        Object m = new Object();
        Owner s = new Owner(g, m);
        Customer c = new Customer(g, m);
        new Thread(s).start();
        new Thread(c).start();
    }
}

class Owner implements Runnable {
    
    
    private Object goods;
    private Object money;

    public Owner(Object goods, Object money) {
    
    
        super();
        this.goods = goods;
        this.money = money;
    }

    @Override
    public void run() {
    
    
        synchronized (goods) {
    
    
            System.out.println("先给钱");
            synchronized (money) {
    
    
                System.out.println("发货");
            }
        }
    }
}

class Customer implements Runnable {
    
    
    private Object goods;
    private Object money;

    public Customer(Object goods, Object money) {
    
    
        super();
        this.goods = goods;
        this.money = money;
    }

    @Override
    public void run() {
    
    
        synchronized (money) {
    
    
            System.out.println("先发货");
            synchronized (goods) {
    
    
                System.out.println("再给钱");
            }
        }
    }
}

insert image description here

directly locked

[4] Interview question: the difference between sleep() and wait() methods

(1) sleep() does not release the lock, wait() releases the lock

(2) sleep() specifies the sleep time, wait() can specify the time or wait indefinitely until notify or notifyAll

(3) sleep() is a static method declared in the Thread class, and the wait method is declared in the Object class

Because we call the wait() method is called by the lock object, and the type of the lock object is any type of object. Then the methods that you want any type of object to have can only be declared in the Object class.

Guess you like

Origin blog.csdn.net/weixin_44226181/article/details/130480194