• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronized Code Behavior

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer methods are synchronized.
 
Tom Purl
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
toString() isn't synchronized.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton Rory! That answers my question exactly.
 
reply
    Bookmark Topic Watch Topic
  • New Topic