Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threading Problem

 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • 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..
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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: 226
Java
  • Mark post as helpful
  • send pies
  • 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: 226
Java
  • Mark post as helpful
  • send pies
  • 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: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
Tony Docherty
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic