• 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

Threads Doubt

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi There,



output: AAAAAAAAAA
BBBBBBBBBB
CCCCCCCCCC


I have a doubt in the program. Here in the line # 1,2,3 there are 3 different objects. the synchronized block locks the currently running object instance variable string buffer sb.
Each object has it's own copy of instance variable sb. According to me, the lock is not unique to the class. In that case, there will be a race condition among three threads. But, how it is preventing the threads from running simultaneously.

Please correct me.

Thanks in Advance
Saritha
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Each object has it's own copy of instance variable sb. According to me, the lock is not unique to the class. In that case, there will be a race condition among three threads. But, how it is preventing the threads from running simultaneously.



Take a look at your code again. Each object has an sb reference, which is assigned in the constructor. All three objects are called using the same string buffer object (passed in the constructor), hence, all three objects refers to the same sb object.

Henry
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi wong,

Thanks

Saritha
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think three different objects are created in line 1,2,3.
can anyone give more explanation?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ganeshkumar cheekati:
i think three different objects are created in line 1,2,3.
can anyone give more explanation?



Here goes. Your a correct that three objects of the class InSync are instantiated. As this class extends Thread calling the start method creates three threads of execution. In the constructor call the InSync objects are passed the reference to single instance of the StringBuffer class. That is the three instances of InSync have a reference to the same StringBuffer object.

You might visualise this as follows (where [:InSync] representes the three anonymous, not named, instances of the InSync class and [sb:StringBuffer] representes the single instance of StringBuffer):



When each thread is executed it's run method acquires the lock of the object [sb:StringBuffer]. As there is only one StringBuffer object each thread executes the code in the run method sequentially.

Hope this clarifies things.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation Graeme
 
Graeme Jenkinson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ankit Garg:
Nice explanation Graeme




Thanks. Your a bit of a guru around these parts so I see this as a sign I'm improving my knowledge. I have the exam booked for the 10th of November
.

BTW what't the latest with your exam?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic