File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

threads ... from K&B

 
Ana Abrantes
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sachin Tendulkar
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a shame !
This is nothing about threads...
Only passing the refence of an object !
Thank you... very much!
Ana
 
K Ville
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic