• 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

Threading

 
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do lines 15-17 do?
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



They are not in loop.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh got it , just passing reference.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

meeta gaur wrote:oh got it , just passing reference.



A copy of the reference : )
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic