Spurious wakeups on windows. Is it possible?

自古美人都是妖i 提交于 2019-12-05 13:47:40

Two things

  1. Spurious wake ups are real, even on Windows. This is documented in the WinAPI: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682052(v=vs.85).aspx
  2. You have a race condition in your test. So, I don't think it's quite accurate.

The race is between the exit of the synchronized block in your worker threads and when they reach processedThreads.incrementAndGet(). Notifier will spin during that time, notifying threads which may or may not have acquired the lock.

In other words

  1. It's possible for Notifier to spin twice (that is, notify() twice) before a worker thread can acquire the mutex.
  2. It's possible for Notifier to spin after the last thread has exited the synchronized block but not yet reached its finally block.

Your two added lines change the output because, by slowing down the Notifier, you're masking the race. (By giving Worker lots of time to enter the mutex.)

Hope that makes some sense.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!