aspose file tools*
The moose likes Java in General and the fly likes Threading Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Threading Problem" Watch "Threading Problem" New topic
Author

Threading Problem

Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223



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..


OCPJP
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223


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

Joined: Feb 06, 2012
Posts: 223

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Aug 07, 2007
Posts: 2406
    
  50
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threading Problem