This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Doubt 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 "Threads Doubt" Watch "Threads Doubt" New topic
Author

Threads Doubt

aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Hi wong,

Thanks

Saritha
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
i think three different objects are created in line 1,2,3.
can anyone give more explanation?


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Graeme Jenkinson
Greenhorn

Joined: Sep 08, 2008
Posts: 27
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.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Nice explanation Graeme


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Graeme Jenkinson
Greenhorn

Joined: Sep 08, 2008
Posts: 27
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?
 
GeeCON Prague 2014
 
subject: Threads Doubt