这个可以吗?在同步块中同步(线程),然后执行thread = null

bob*_*obo 5 java blackberry synchronized-block

我看到了这个:

// 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?
}
Run Code Online (Sandbox Code Playgroud)

是否可以设置thread=nullwhile仍然保持锁定?

我在一些BB代码中找到了这个金块.

Jon*_*eet 7

是的,没关系.synchronized语句将获取其锁定的引用的副本,并使用该副本计算出最后解锁的内容.

Java语言规范的第14.19节实际上并不清楚这一点,但它确实声明表达式在开始时进行了评估 - 并且未提及稍后再次对其进行评估.