我看到了这个:
// thread is a member of this class
synchronized( this.thread )
{
this.thread.running = false;
this.thread.notifyAll(); // Wake up anything that was .waiting() on
// the thread
this.thread = null; // kill this thread reference.
// can you do that in a synchronized block?
}
是否可以在设置 thread=null
的同时保持对它的锁定?
我在一些 BB 代码中发现了这个金 block 。
最佳答案
是的,没关系。同步语句将获取它所锁定的引用的副本,并使用该副本计算出最后要解锁的内容。
Section 14.19 Java 语言规范的定义实际上并不清楚这一点,但它确实声明表达式在开始时被求值——并且没有提到稍后再次求值。
关于java - 这个可以吗?同步(线程),然后同步块(synchronized block)中的线程=空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4226827/