This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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'm still in control here. LOOK at this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic