当前位置:网站首页>Multithreading Basics (II)

Multithreading Basics (II)

2022-06-09 06:39:00 L.S.V.

Reentrant lock

When a thread requests a lock held by another thread , The requesting thread will enter a blocking state

Because the built-in lock is reentrant , So if a thread tries to acquire a lock that is already held by itself , This request can succeed


public class ReloadDemo {
    private Object childLock = new Object();

    public void doSomething(){
        synchronized(childLock){
            System.out.println("Do something.");
            this.doSomethingAgain();
        }
    }

    public void doSomethingAgain(){
        synchronized(childLock){
            System.out.println("Child do something  .");
        }
    }

    public static void main(String[] args) {
         ReloadDemo reloadDemo = new ReloadDemo();
         reloadDemo.doSomething();
    }

}

原网站

版权声明
本文为[L.S.V.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090626410996.html