• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

threads ... from K&B

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 do some of my very best work in water. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic