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.