• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Threading Problem

 
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


can someone please explain me this code.. how can synchornized methods is executed by both the threads.. output is 5 ,4, 3 ,2, 1..
cant understand the flow of the code..
 
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

Ishan Pandya wrote:can someone please explain me this code.. how can synchornized methods is executed by both the threads.. output is 5 ,4, 3 ,2, 1..
cant understand the flow of the code..



First, the output can not be "5,4,3,2,1..". You don't print out any commas, and you do printout thread ids, names, and new lines. Giving us incorrect or summarized output is worse than giving no output since it specifically changes observed behavior. And you seem to be asking about observed behavior.

Why can two threads access the synchronized method? You have two instances of the Hold object. When you create a synchronized method you synchronize on the instance, since you have more than one instance you have more than one lock and more than one thread can run. If you want to prevent interleaved execution you need to synchronize on an object which is shared between all the threads.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why can two threads access the synchronized method? You have two instances of the Hold object. When you create a synchronized method you synchronize on the instance, since you have more than one instance you have more than one lock and more than one thread can run. If you want to prevent interleaved execution you need to synchronize on an object which is shared between all the threads.



I really sorry man. I just forget to remove the "println" statements from the code. i just kept that for my testing purpose.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wanted to ask that is the output Predictable like if we just consider the println statement on line 26.. is it that the output will always be in descending order such as 5 4 3 2 1?
 
Steve Luke
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

Ishan Pandya wrote:I just wanted to ask that is the output Predictable


No, the output is not predictable because the two threads are not synchronizing with each other.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is no.

When running on a modern computer with 200ms delays in the code and no other applications running in the background the probability is you will get what appears to be deterministic behaviour. But as you reduce the time of the delays and/or the performance of the computer you will find it will get to a point where it fails to even appear deterministic. Just try removing the delays totally and see what happens.

EDIT: Sorry Steve, hadn't spotted you'd already answered this.
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic