Use wait() and notify() for thread communication

http://ideone.com/yOMIBR