• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to avoid dead lock?

 
Eric Kim
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implemented lock() / unlock() in this way but find it has serious issue:

1. If the client successfully finished lock/update/unlock cycle, then regardless of how many client threads access it, data class works well, do data corruption or dead lock found.
2. If client crashes (For instance, I intentionally terminate a running multiple threaded client 20 threads all are doing lock/update/unlock cycle on one record), then next time client starts up, the record locked by previous run remains locked.
The problem I found is really Data class has no way to identify which client actually is using it. I tried to put Thread.currentThread() into lockedRecords and call isAlive() to determine whether client actually died, but it is not working, I guess this is probably because RMI maintains a threadpool or some sort and reuse thread, so after a while it will cause dead lock in that case.
I am considering:
1) change wait() to wait( 5000 ), after 5 seconds it will timeout.
2) For each locked record, it also maintains duration while it has been locked. If it is locked for more than 5 seconds, lock will automatically expired and it is removed from lock.
With this approach I hope it might solve client crash gracefully instead of hanging forever.
Can someone please offer some suggestion on this approach? Is dead lock detection actually required in the assignment at all? It is not mentioned directly in my instruction though.
[Andrew: put the source code between [code] and [/code] UBB tags, and removed unlock() method]
[ March 16, 2004: Message edited by: Andrew Monkhouse ]
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some of your questions have to do more with your overall design rather than locking issues. How to detect invalid locks is an issue depending on whether you use a 2 or 3 tier approach.
I'm not very fond of your use of the while loop to determine when a record can be locked. It's cpu intensive and not the best use of Java.
I used a 3-tier approach, so I didn't face any of the issues that you are facing. Of course, my primary motivation for going that route was so I didn't have to bother with client connections and everything else.
 
Eric Kim
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nathaniel,
Why you think it is cpu intensive for a while loop like that:
while( lockedRecords.containsKey( record ) )
{
lockedRecords.wait();
}
If it runs into wait(), it does not consume any CPU time at all. I can certainly change it if.
My second question is how does your 3 tier look like? When I got the instruction the client tier and then network server tier looks more natural, but if your design works better and can resolve this dead lock issue, can you shed some lights?
Thanks,
Eric
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,
I have edited your post to put the code between [code] and [/code] UBB tags. Doing this ensures that indenting is preserved, which makes the code easier to read.
When you are writing your post, there are a number of buttons just below the edit pane, which will insert the tags for you, so you don't have to remember what they are.
If you would like to edit your original post so that you can see what I have done, you can click on the button that is just above your post.
I have also removed some of your posted code, as we do not allow major sections of the assignment to be posted. The locking code is worth 20% of the assignment, and this is far too much to post in total. The question "What is the policy on posting questions I saw on the exam / details of how to do the assignment?" is address in the JavaRanch SCJD FAQ.
I have left the lock method, since this is being discussed. But if you wanted to discuss the lock and unlock methods together, then I would recommend you write up the relevant sections as psuedocode so you can discuss them. Do not post both methods please.
do data corruption or dead lock found.

As Nathaniel said - this is more an issue with your overall design. It is not possible to tell this from just looking at your lock and unlock methods.
If client crashes (For instance, I intentionally terminate a running multiple threaded client 20 threads all are doing lock/update/unlock cycle on one record), then next time client starts up, the record locked by previous run remains locked.

I believe you do not have to worry about fixing this for this assignment - as long as you document that you have considered it and are not going to fix it (and why).
To get around that you could look at using an RMI connection factory. That way each client will get a unique remote object that they can use to lock records. You would then have two choices:
  • Implement the Unreferenced interface on the unique remote objects. Sometime after a client dies the unreferenced method will be called, and you can unlock any records that were owned by the dead client.
  • Keep using unique classes all the way down to the locking code (so each client would have I am considering:

  • 1) change wait() to wait( 5000 ), after 5 seconds it will timeout.
    2) For each locked record, it also maintains duration while it has been locked. If it is locked for more than 5 seconds, lock will automatically expired and it is removed from lock.a unique instance of Data class) and then use the instance of the Data class in a WeakHashMap as part of your locking solution. Sometime after a client dies the entry will be removed from the WeakHashMap.
    (This solution could be quite challenging to implement since you also have to track cookies).


    I am considering:
    1) change wait() to wait( 5000 ), after 5 seconds it will timeout.

    Do your instructions allow this? Don't you have an instruction that the call to lock() must block until the lock is granted?
    What happens after that 5 seconds timeout? I assume you are planning on throwing some sort of exception - so what exception is it? And what should the client expect?
    Regards, Andrew
    [ March 16, 2004: Message edited by: Andrew Monkhouse ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic