• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on Synchronized mehods

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization.
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic