• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Trouble with synchronized keyword

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
I am preparing for the SCJP test and I have trouble understanding the usage of the synchronized keyword. In the following code i am attempting to create three threads and synchronize the run method such that 100 As Bs and Cs will be printed unbroken on one line. But the trouble is that the output is getting all jumbled up. Am I right in thinking that since the run method is synchronized, once a thread has entered it, no other thread can enter until the first thread had exited the method. Can anyone please tell me where I'm going wrong.
Thanks in advance.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Varun. Welcome to The Ranch!

Two threads that are synchronized on the same object cannot run the method at the same time. So the question is, what object are the threads synchronized on?
 
Gvarun Rao
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew. I rewrote the above code as follows. I have synchronized on any instance of the StringBuffer class so that the threads can lock onto instances of the StringBuffer which they all use. Now the program prints unbroken lines of As,Bs and Cs but the order in which they are printed is not fixed. How can I fix it such that As,Bs and Cs are printed in order.

 
Ranch Hand
Posts: 109
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Varun,

By adding synchronized keyword to run method its synchronize that InSync thread objects run method only. So that's not effect on other InSync thread objects. But it will effect if some threads invoke one specific InSync instance run method just as simple method invocation.

There are three ways to make this code work correctly.

Here is one and easy way. Study it.
 
Gvarun Rao
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Udara. Your help is much appriciated.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic