线程不会从其他线程继承锁,这里发生了其他事情.
在您的示例中,正在运行methodA的线程必须先对spawnedThread进行锁定,然后才能进入同步块.
然后,当spawnedThread运行时,它必须获取自身的锁,才能在run方法中输入同步块.
因此methodA线程具有锁,而spawnedThread试图获取相同的锁.但这不会死锁,因为Thread.join在放弃锁的位置执行等待,请参见api documentation for Thread.join:
This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
没有超时值的Thread.join版本将锁定它所加入的线程,将其放弃并进入休眠状态.它不会唤醒,直到发生以下情况之一:
1)被加入的线程完成(发送唤醒等待线程的通知)
2)连接线程被中断(意味着某些调用在其上中断,在此示例中不会发生)
3)连接线程自行唤醒(“虚假唤醒”,这是竞争条件导致的,这是罕见的)
我尚不清楚您将如何更改锁以在此处描述的僵局,如您在评论中所述,如果您希望得到答案,请将该代码版本添加到您的问题中.