"Huge bug" 在受保护的区块中 Java
"Huge bug" in Guarded Blocks Java
IBM 教程 5 things you didn't know about ... java.util.concurrent, Part 1 states that Java tutorial Guarded blocks 示例包含一个“巨大错误”,但未指定它:
Bug watch!
By the way, you're right if you noticed that Guarded Blocks contains a huge bug — what would happen if a developer synchronized on the Drop instance inside of main()?
谁说得对?
如果错误已在 Java 教程中修复,则它发生在 2010 年 5 月至 2011 年 11 月之间,因为 WebArchive.
中的代码看起来相似
上面的答案(Artur 和 Gedrox)是正确的——在放置对象本身上同步是违反封装的,因为任何人(从外部)也可以获得该监视器,从而完全破坏信号机制.
我已经有一段时间没有看过 Java 教程代码了,但我确实记得在写这篇文章时看过它。如果他们修好了,那是我看过之后。
至于这是不是"huge bug",我坚持是的说法;任何可能破坏同步方案的事情都应该避免,因为追踪它需要非常熟练地使用 Java 同步系统,一个持续导致死锁的代码路径,并且非常熟悉所有可能的排列,以便人们可以在调试环境之外对其进行推理。这不是成功的秘诀。
IBM 教程 5 things you didn't know about ... java.util.concurrent, Part 1 states that Java tutorial Guarded blocks 示例包含一个“巨大错误”,但未指定它:
Bug watch!
By the way, you're right if you noticed that Guarded Blocks contains a huge bug — what would happen if a developer synchronized on the Drop instance inside of main()?
谁说得对?
如果错误已在 Java 教程中修复,则它发生在 2010 年 5 月至 2011 年 11 月之间,因为 WebArchive.
中的代码看起来相似上面的答案(Artur 和 Gedrox)是正确的——在放置对象本身上同步是违反封装的,因为任何人(从外部)也可以获得该监视器,从而完全破坏信号机制.
我已经有一段时间没有看过 Java 教程代码了,但我确实记得在写这篇文章时看过它。如果他们修好了,那是我看过之后。
至于这是不是"huge bug",我坚持是的说法;任何可能破坏同步方案的事情都应该避免,因为追踪它需要非常熟练地使用 Java 同步系统,一个持续导致死锁的代码路径,并且非常熟悉所有可能的排列,以便人们可以在调试环境之外对其进行推理。这不是成功的秘诀。