Hi guys, I've written a class Printer that,I thought, will notify the "main thread" about the fact that it wrote a message onto a screen. However, the program doesn't behave as I expected. I expected to see results like this:
BBB Another thread printed a message onto a screen BBB Another thread printed a message onto a screen BBB Another thread printed a message onto a screen and so on(10 such pairs)
while I most often get: BBB BBB . . . (ten times) Another thread printed a message onto a screen Program complete.
I tried debugging it and observed strange Printer object's lock exchange between the two threads.
Could anyone please help? I really have no idea why it gives such results.
Thanks Henry for your reply. I understand and agree with your explanation in points 2 and 3. As far as 1 is concerned, debugger showed me things that could justify your explanation. The main thread was very rarely on the Printer object's monitor. But how my wait() loop gives up the lock without calling wait()? I still don't how to fix this. Could you suggest some code changes so that I can undestand it thoroughly? Thanks.
The synchronized block in main() should be around the loop, not inside. Then the only way that control is surrendered to the Printer thread is by calling wait(). Otherwise, the Printer thread can do whatever it wants when the control part of the loop is executing outside the synchronized block, and that's when notifications can be lost.
A multithreaded program will act completely differently under a debugger, because stepping in a debugger obviously changes the timing of everything by a lot!
But how my wait() loop gives up the lock without calling wait()? I still don't how to fix this.
Take a look at your loop....
As you see, you have the synch block inside the while loop. And there are periods just before and just after the sync block, yet within the loop, where the thread doesn't hold the lock, and not calling wait(). The fix is, of course, to have the while loop inside the sync block.
Could you suggest some code changes so that I can undestand it thoroughly? Thanks.
Well, you also shouldn't be waiting blindly. It may be better to have some state on which thread is supposed to print -- which gets toggled back and forth as the two threads are printing.
Joined: Jul 05, 2008
I've done like you said but there is no improvement. Additionally, the main thread does not die (it doesn't print 'Program complete').