Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threading

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A Thread-2
A Thread-2
A Thread-2
A Thread-2
A Thread-2
A Thread-2
A Thread-2
A Thread-2
A Thread-0
A Thread-1
A Thread-0
A Thread-0
A Thread-0
A Thread-0
A Thread-0
A Thread-2
A Thread-0
A Thread-0
A Thread-0
A Thread-0
B Thread-2
A Thread-1
C Thread-1
C Thread-1
C Thread-1
C Thread-1
C Thread-1
C Thread-1
C Thread-1
C Thread-1

It's really strange , i thought all threads will print A only ,didn't get from where they got B and C ?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do lines 15-17 do?
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:What do lines 15-17 do?


They are not in loop.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No they aren't, but they don't need to be. All threads are acting on the same buffer. One of them changes the buffer they all see it. The code is not synchronized (properly) so the results are unpredictable (as seen by the intermixing of threads and the advancement from A to B back to A then directly to C). Even if the code was properly synchronized, you shouldn't expect all threads to display 'A'. What should you expect in that case?
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought i have created three different threads and they use their own lock for synchronization and operating on their own StringBuffer object rather than a shared object. sb is not shared object , each class object has their own.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meeta gaur wrote:I thought i have created three different threads
Yes.
and they use their own lock for synchronization
Yes
and operating on their own StringBuffer object rather than a shared object.
No. How many times do you make a StringBuffer? What line(s) of code make the StringBuffers?

And because the third statement is false the fact that the first and second are true breaks the code.
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh got it , just passing reference.
 
Aaron Shawlington
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meeta gaur wrote:oh got it , just passing reference.


A copy of the reference : )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic