File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads Doubt

 
aslika bahini
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi wong,

Thanks

Saritha
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think three different objects are created in line 1,2,3.
can anyone give more explanation?
 
Graeme Jenkinson
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation Graeme
 
Graeme Jenkinson
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic