持有多重锁定的线程进入wait()状态.它会释放所有保持锁吗? [英] A thread holding multiple lock goes into wait() state. Does it release all holding locks?

查看:659
本文介绍了持有多重锁定的线程进入wait()状态.它会释放所有保持锁吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个程序来检查线程t1是否在两个不同的对象上持有锁: Lock.class和MyThread.class使用MyThread.class.wait()在MyThread.class实例上进入等待模式.它不会释放Lock.class实例上的锁.为什么这样 ?我一直在想,一旦线程进入等待模式或死亡,它将释放所有获得的锁.

I wrote this program to check if a thread t1 holding lock on two different objects : Lock.class and MyThread.class goes into waiting mode on MyThread.class instance using MyThread.class.wait().It does not release lock on Lock.class instance. why so ? I have been thinking that once a thread goes into wait mode or it dies it releases all the acquired locks.

public class Lock {

protected static volatile boolean STOP = true;
public static void main(String[] args) throws InterruptedException {
    MyThread myThread = new MyThread();
    Thread t1 = new Thread(myThread);
    t1.start();
    while(STOP){
    }
    System.out.println("After while loop");
    /*
     * 
     */
    Thread.sleep(1000*60*2);
    /*
     * Main thread should be Blocked.
     */
    System.out.println("now calling Check()-> perhaps i would be blocked. t1 is holding lock on class instance.");
    check();
}

public static synchronized void check(){
    System.out.println("inside Lock.check()");
    String threadName = Thread.currentThread().getName();
    System.out.println("inside Lock.Check() method : CurrrentThreadName : "+ threadName);
}
}


class MyThread implements Runnable{
public MyThread() {
}

@Override
public void run() {
    try {
        System.out.println("inside Mythread's run()");
        classLocking();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static synchronized void classLocking() throws InterruptedException{
    System.out.println("inside Mythread.classLocking()");
    String threadName = Thread.currentThread().getName();
    System.out.println("inside MyThread.classLocking() : CurrrentThreadName : "+ threadName);
    /*
     * outer class locking 
     */
    synchronized (Lock.class) {
        System.out.println("I got lock on Lock.class definition");
        Lock.STOP = false;
        /*
         * Outer class lock is not released. Lock on MyThread.class instance is released.
         */
        MyThread.class.wait();
    }
}
}

推荐答案

您是正确的,它不会释放其他锁.至于原因,是因为这样做并不安全.如果可以安全地在调用内部函数期间释放外部锁,为什么要在完全保持另一个锁的情况下调用内部函数?

You are correct that it doesn't release the other lock. As for why, it's because it isn't safe to do so. If it was safe to release the outer lock during the call to the inner function, why would the inner function be called with the other lock held at all?

拥有一个函数可以释放一个程序员没有获得的锁,这将破坏同步函数的逻辑.

Having a function release a lock it didn't acquire behind the programmer's back would destroy the logic of synchronized functions.

这篇关于持有多重锁定的线程进入wait()状态.它会释放所有保持锁吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆