permaculture playing cards*
The moose likes Threads and Synchronization and the fly likes Question on Synchronized mehods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Question on Synchronized mehods " Watch "Question on Synchronized mehods " New topic
Author

Question on Synchronized mehods

Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Hi ,
I ran the following code from the java tutorial on Concurrency. This code's objective is to implement 'deadlock'.




I have added some debug messages for a better understanding the flow during runtime.
I have added a static field 'status' and I just update it to check the synchronized access.

In my understanding so far when a Synchronized method is invoked by a thread ,no other thread can invoke the same
method until the first thread that has the lock over the method returns the lock.

But I get the following output: (it actually results in deadlock)


Thread[Thread-0,5,main] setting the status to 1
Thread[Thread-1,5,main] setting the status to 2
Alphonse: GastonThread[Thread-0,5,main] has bowed to me!
Gaston: AlphonseThread[Thread-1,5,main] has bowed to me!

From the output I see that Thread-1 is able to set the status before Thread-0 completes!
How does synchronization work ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

In my understanding so far when a Synchronized method is invoked by a thread ,no other thread can invoke the same
method

... of the same object. Calling a method of an object from several threads is not the same as calling a method of different objects from several threads. If you were calling alphonse.bow(gaston); in both threads, the result would be what you expect.


[My Blog]
All roads lead to JavaRanch
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Thanks Christophe.
Le me try to understand this at a more abstract level.
Consider the scenario in which an account is jointly by Customer A and Customer B.
When Customer A and Customer B try to update the account balance at the same time,it will be a case of
" calling a method of different objects from several threads "
So will Customer B allowed to read the account balance before Customer A updates it ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Moving to Threads and Synchronization.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Continuing with my earlier question,here is a piece of code i tried to understand synchronization:


I get the following output:
Thread[updateThread,5,main]Before deposit balance10
Thread[WithDrawThread,5,main]Before withdraw balance10
Thread[WithDrawThread,5,main]After withdraw balance10
Thread[updateThread,5,main]After deposit balance10


I don't understand this output,can somebody please explain ?
Is it the case of withdrawal done before the synchronized deposit is completed ?

Thanks
Yogi
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Hi,

In your code method deposit() is synchronized, while withdraw() is not.
Probably two threads started at the same time, first thread entered deposit() method and printed 10,
while the other thread at the same time entered whithdraw() method and printed 10 too.
Then the first thread increased balance by 1 then entered wait(), while the other at the same time (a few nanosecond earlier or later)
decreased balance by 1 , then printed "After withdraw balance" - 10.
At the end the first thread printed "After deposit balance"-10.

This is an example of race condition - the access to 'balance' is not synchronized in withdraw() method.
In some (rare) cases you could even get final balance = 9 or 11 in this code (this could be very rare, but if you run this cone 10 mln. times in a loop,
I think this would happen a few times).
To prevent this you must synchronize withdraw() method too.


Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Probably two threads started at the same time, first thread entered deposit() method and printed 10,
while the other thread at the same time entered whithdraw() method and printed 10 too.


Since I have synchronized deposit() ,will it not be locked ?
How can some other thread read balance when the synhronized deposit() has not completed ? How does the other thread obtain the lock to read 'balance' ?

Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Ok, I am able to understand something now..
the thread that runs 'withdraw()' will be able to read balance....its only the method that is locked and not the variable..
I still need to practice more code to understand the nuances of synchronization.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on Synchronized mehods