79637073

Date: 2025-05-24 18:43:54
Score: 2.5
Natty:
Report link

The problem is that you use `if` instead of `while` before calling `wait()`. Due to spurious wakeups, threads can wake without a `notify()`, so you must re-check the condition in a loop to avoid incorrect behavior. This causes more than the intended number of threads to run simultaneously in your code. Using a `while` loop to guard `wait()` ensures the condition is truly met before proceeding. For a detailed explanation, see this article:

https://vijay-belwal.hashnode.dev/i-thought-i-understood-wait-until-my-thread-woke-up-without-a-notify

Reasons:
  • Blacklisted phrase (1): this article
  • No code block (0.5):
  • Low reputation (1):
Posted by: Vijay Belwal