Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem of multi-objects synchronisation

 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone explain what�s the problem in the code below please? Deadlock or? Thank you very much in advance.

Regards,
Ellen
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deadlock problems only occur when locks need to be acquired on more than one resource, and when those locks are not acquired in a fixed order. In this case, the only place where this happens is the transfer() method: the method itself needs one lock, while the source.withdraw(amount) call needs the second.
So can a deadlock arise? Well, when there are two AccountAccess objects, call them a and b, then a.transfer(100, b) will acquire the locks in a different order from b.transfer(100, a).
So how does the deadlock occur? You will need to have the two calls above executing simultaneously. The a.transfer(100, b) thread acquires a lock on a; let's assume you get a context switch immediately afterwards before the thread reaches the if statement. The other thread now executes b.transfer(100, a) and acquires a lock on b, then attempts to call a.withdraw(amount) but blocks because our first thread still holds the monitor lock. But our first thread won't run either because on the call to b.withdraw(amount) it will not get b's monitor lock. Deadlock.
The simplest solution is to ensure that the order in which locks are acquired is fixed. For example, if each account has an account ID, you could sayThis code always acquires the two locks in order of ascending account ID and thereby avoids deadlock.
- Peter
 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Mr. Haan,
Thank you very much for your explicit explaination! My favourite bartender Peter den Haan.
I guess there might be a typo in your code above, the " private void transfer(...)" seems should be " private void performTransfer(...) ", right?
Best Regards,
Ellen
[ January 12, 2003: Message edited by: Ellen Fu ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Mr. Haan,
Peter's probably too modest to point this out, but it's actually Dr. den Haan. At least, I think you include the "den" part without capitalization; not sure.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's a typo.
Jim's right. My surname is "den Haan", where "den" is a prefix that is not normally capitalised. Lexicographically, the "Haan" is more significant than its "den" prefix, so in a sorted list I would normally expect to find my surname somewhere under "H".
As you can imagine, my life here in the UK is rife with lexicographical disappointments
I don't normally bother with the "Dr." bit; nor do I put "Author of this and that" in my .sig (new JSP 2.0 book coming out in March, co-authored by yours truly, by the by ). My arguments ought to make sense and be convincing, irrespective of any title. And conversely, when I spout nonsense nothing should get in the way of someone else's honest appraisal of that fact
In any case, glad I could be of help.
- Peter
[ January 12, 2003: Message edited by: Peter den Haan ]
 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sehr geehrter Dr. den Haan,
I�m sorry for my mistake. I am not very familiar with western names. When I saw your name for the first time, I thought you might from Holland or Sweden or somewhere else, den Haan is really a rare British name to me.
Your new book on JSP will be available soon? Great, I think I am going to get it. I have a book on JSP 1.1 written by Hans Bergsten, it�s been translated into my native language Chinese and but some terms sound very weird to me when translated( I learnt everything on CS from very beginning in English or German). I�d like to get an English one for the latest version of JSP, your book highly expected.

mit freundlichen Gr��en,
Ellen
P.S. Thanks to Mr. Yingst for pointing out my mistake. I�m always glad to learn.
[ January 12, 2003: Message edited by: Ellen Fu ]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well spotted: it's a Dutch name. Dutch by birth, but hopped across the channel a few years ago.
I wrote two chapters about web application architecture and Struts for a "Beginning JSP 2.0" book for Wrox. The reviewers all tell me I'm overtaxing my readers---but you seem to be the persevering type so you might enjoy it
Beeindruckend. Auf Deutsch geht's noch, aber meine Chinesische Vokabul�r is beschr�nkt zu ein oder zwei Worte der ein Freund (ex-Hongkong-Polizist) mir gelernt hat
Enough banter. Back to your regular scheduled thread, erm, programme---
- Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic