Originally posted by Cory Max:
... Would this make it wait forever? ...
Indeed, that's exactly what happens when I run this code. (I need to press Ctrl+C to get the command prompt back.)
I'm using a Mac, which I've noticed to be very quick to switch between threads. So when b.start() is called, b is
usually given a chance to run before the next line in the main thread executes. I expect that on a Windows machine, the main thread would
usually continue to the next line and start waiting before b is given a chance to run.
You could force this scenario to happen by inserting a call to sleep after b.start() is called. This would give b a chance to finish running and call notify
before wait is called in the main thread. (See code below.)
Note that you can also specify a maximum amount of time to wait, so that it will
not wait forever. In the code below, I've inserted a 3-second timeout so that the program does not freeze up.