This is from Chapter 9 of the Kathy/Bert book:
This program prints the letter "a" 100 times, followed by "b" 100 times, follwed by "c" 100 times. It works just as it should according to the book.
If I understand this correctly, all of the code in the run() method is synchronized on the letter object, which is of type StringBuffer. This means that if any
thread calls a synchronized method of the letter object, then all other threads will be locked out of all of the synchronized methods for that particular object until the first thread is no longer using that code block. Am I right so far?
Ok, then if that's true, then why does only one thread ever run the following lines of code at one time?
The System.out.print() method ends up invoking the toString() method of the letter object, which is a StringBuffer. But the toString() method
isn't synchronized. Isn't it possible that more than one thread could be running this block of code since it doesn't include any synchronized methods of the locked object? Also, this code is called before any synchronized methods of the locked object are called?
Thanks for any help!
[ May 06, 2003: Message edited by: Tom Purl ]
[ May 06, 2003: Message edited by: Tom Purl ]