Wait() and notify() behavior question


#1

If a thread isn’t currently waiting but notify() is called on it, then the next time that thread calls wait(), it’ll return from the wait immediately and continue to run.

What happens if notify() is called on the thread twice while it isn’t waiting? Does the thread return immediately the next two times it calls wait(), or does it only return immediately the first time and then reset the signal?


#2

It’s just a flag, not a counter, so it doesn’t matter how many times you call notify(), it will only trigger it once.


#3

OK, thought so, but wanted to make sure. Thanks!


#4

One more question: say that a Thread 1 calls notify() on another Thread 2 which isn’t currently waiting, but which isn’t schedule to run next either. Does calling notify() on Thread 2 update its scheduling so that it runs next? Or do threads simply continue to run as scheduled, with Thread 2 getting its signal set so that the next time it calls wait() it just returns immediately?

Thanks again for the help,
Mike


#5

The OS may prioritise threads that are waiting on a signalled event, but there’s no reason to assume that. (Doesn’t really matter anyway, TBH)


#6

Alright, thanks. I was just curious if signaling a thread a lot could interfere with scheduling in some way.