aspose file tools*
The moose likes Beginning Java and the fly likes Threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Threading" Watch "Threading" New topic
Author

Threading

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305


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 ?


My one regret in life is that I am not someone else.
- Woody Allen
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

What do lines 15-17 do?


Steve
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Steve Luke wrote:What do lines 15-17 do?


They are not in loop.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Dec 05, 2012
Posts: 305

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Dec 05, 2012
Posts: 305

oh got it , just passing reference.
Aaron Shawlington
Ranch Hand

Joined: Oct 01, 2013
Posts: 31
meeta gaur wrote:oh got it , just passing reference.


A copy of the reference : )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threading