you start 2 threads: Producer and Consumer and they call different method: put and get, this method synchronized on instance of Q.
you cant be sure what method will be called first.
but boolean value = false;
and test inside you can be sure that thread that call get will wait if it called first.
thread will wait for notify and getting lock, and that check condition again.
so when other thread get lock:
will check condition, but this time no wait will execute because initially while value = false.
you set n variable and change value to true and notify other thread if any in waiting state.
if reverse order of starting you will have same behavior.
Hope this help.