File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes threads ... from K&B 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 ... from K&B" Watch "threads ... from K&B" New topic

threads ... from K&B

Ana Abrantes
Ranch Hand

Joined: Sep 04, 2003
Posts: 43
Exercise 9-2 from Kathy's book:

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<p>SCJP 1.4
Sachin Tendulkar

Joined: Sep 20, 2003
Posts: 27
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.
Ana Abrantes
Ranch Hand

Joined: Sep 04, 2003
Posts: 43
What a shame !
This is nothing about threads...
Only passing the refence of an object !
Thank you... very much!
K Ville
Ranch Hand

Joined: Aug 16, 2003
Posts: 58
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
I agree. Here's the link:
subject: threads ... from K&B
It's not a secret anymore!