I didn't understand yet why each thread have a different letter if the letter passed is the same... Since they're new objects of InSync (not the same one), shouldn't they have the same "A" ? I'm very confused with threads... Ana
There is only one instance of the StrinfBuffer and its contents are the letter "A". Three instances of the class InSync are created and each is passed a reference to this single StringBuffer. The run method is synchronized on this StringBuffer object which is shared by all the three instances of InSync. Only one thread can therefore execute the run method. The run method prints the StringBuffer 100 times and then sets the first character of the StringBuffer to the next value ie. B in this case. The next thread then does the same thing. Hence the output.
Pardon me for my ignorance, but I seem to have the same confusion when I first saw this on the book. I guess I need a detailed explanation on this. That is, if anyone too patient would do so. Thanks in advance and God bless.
Thinking is the talking of the soul with itself...-Plato
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, that will fit in a stocking