GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronized Code Behavior Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronized Code Behavior" Watch "Synchronized Code Behavior" New topic
Author

Synchronized Code Behavior

Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
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 ]

Tom Purl<br />SCJP 1.4
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
StringBuffer methods are synchronized.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
toString() isn't synchronized.
Rory French
Ranch Hand

Joined: Apr 03, 2003
Posts: 97
Hi Tom, I'm going to review your assumptions :

all of the code in the run() method is synchronized on the letter object, which is of type StringBuffer

correct, I agree.

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.

Also true.
But remember also that only one thread can enter a synchronized block at a time, no matter what the object being synchronized on is. So in this case the thread safety of StringBuffer's methods is not really relevant. The for loop is therefore only run by one thread at a time because only one thread can enter the synchronized(this.letter) code block at a time.
If synchronized(this.letter) were change to synchronized(this) for example, the result would still be the same.
Hope this helps
Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
Thanks a ton Rory! That answers my question exactly.
 
GeeCON Prague 2014
 
subject: Synchronized Code Behavior