Help coderanch get a
new server
by contributing to the fundraiser
  • 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

K&B Book, Thread question

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below is from K&B's book, it will print out 100 As, then 100 Bs, finally 100 Cs. My question is at line 23-25, three anonymous objects are created and they are different objects, and since "letter" is an instance variable rather than an class variable, there should be three "letter" variables on three separated stack, the operation in run() method should not affect the "letter" in others' stacks, therefor it should print 300 As. But they do print 100 As, then 100Bs, then 100Cs, could anybody tell me why? Thanks in advance.
1. class InSync extends Thread {
2. StringBuffer letter;
3.
4. public InSync(StringBuffer letter) {
5. this.letter = letter;
6. }
7.
8. public void run() {
9. synchronized(letter) {
10. for(int i = 1;i<=100;++i) {
11. System.out.print(letter);
12. }
13. System.out.println();
14. // Increment the letter in StringBuffer:
15. char temp = letter.charAt(0);
16. ++temp;
17. letter.setCharAt(0, temp);
18. }
19. }
20.
21. public static void main(String [] args) {
22. StringBuffer sb = new StringBuffer("A");
23. new InSync(sb).start();
24. new InSync(sb).start();
25. new InSync(sb).start();
26. }
27. }
[ August 31, 2003: Message edited by: Dep Joy ]
[ August 31, 2003: Message edited by: Dep Joy ]
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dep,
Yes, there are 3 instances of InSync, so as you've said, there will be 3 instances of the member variable letter. But all the instances of this letter is pointing to only one object (the StringBuffer sb), which is passed to the constructor of InSync at lines 23, 24 & 25. In short, all 3 threads are working on 1 object only.
Hope this helps.
[ August 31, 2003: Message edited by: Alton Hernandez ]
 
Dep Joy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much! It seems I need to do more work on the by value and by reference section, I thought I had grasped it
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic