aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt regarding Concurency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt regarding Concurency" Watch "Doubt regarding Concurency" New topic
Author

Doubt regarding Concurency

Dragos Nica
Ranch Hand

Joined: Oct 25, 2009
Posts: 39
Hi all,

I have doubt regarding the following code:
The source of it is http://www.certpal.com -> Concurency-> Question nr 6



I thought that the answer is 0 1 2 0 1 2 , but the author says :
"There are 2 threads in action and both of them have access to the var variable since it is static.
Both of them instantiate the counter to 0. There is no change in the value of the var variable.The output cannot be guaranteed since
there is no synchronization to go with the execution.There is nothing to prevent both threads from accessing gun at the same time."

I think this is wrong because the threads object are constructed using two different instances of the Trans (line 1 and 2)
Also I run the program and the output was 0 1 2 0 1 2

Can somebody please explain?

Thanks


SCJP 6.0 (88%)
Rein de Boer
Greenhorn

Joined: Jul 12, 2009
Posts: 15
because var is static, all the different instances access the same variable, so the explanation is right.
Dragos Nica
Ranch Hand

Joined: Oct 25, 2009
Posts: 39
Rein de Boer wrote:because var is static, all the different instances access the same variable, so the explanation is right.


I agree with you, but the "var" variable is never modified, it has 0 value during all execution, so why can't be the output predictable?
Ok ..I think I understood ...as both threads are running the output can be 00 11 22 for instance ...now I understand why the output cannot be predictable, but I still don't understand what role has the var variable in this?
It is used only for initialize the counter variable...
Rein de Boer
Greenhorn

Joined: Jul 12, 2009
Posts: 15
you're right, that var is always 0.
so it has got no impact on the outcome in fact.

the issue is 2 threads, running together, with no synchronization, each outputting the counter values in order. so the output of the values is indetermined because of that, because you have no guarantee when and for how long each thread runs.
if gun() was synchronized, then there would be a predefined order.
Dragos Nica
Ranch Hand

Joined: Oct 25, 2009
Posts: 39
I think the situation will be the same also if the method gun is synchronized because both threads will obtain the locks.
gun will be synchronized on "this" and there are two instances:


Hope that I am right ...because I'm confused now..
Rein de Boer
Greenhorn

Joined: Jul 12, 2009
Posts: 15
you're right.
to make synchronization work, you would have to make the method static and then synchronize, or create a specific synchronization block in the function and lock on the var object (which is also static).
Dragos Nica
Ranch Hand

Joined: Oct 25, 2009
Posts: 39

I think that the simplest way is to use the same instance f when create the threads object and to make method gun synchronized.




Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding Concurency