• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

implementing lock timeouts - B&S

 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I use:

private static final Vector LOCKED_RECORDS = new Vector();

to keep track of all the records which are currently locked. The core of my lockRecord(long recNo) method looks like this:

synchronized(LOCKED_RECORDS)
{
while (LOCKED_RECORDS.contains(lock))
{
try
{
LOCKED_RECORDS.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
LOCKED_RECORDS.add(lock);
}

I would like to improve this so that a thread doesn't wait() forever for a record to be unlocked. I'm considering using wait(long timeout) instead, but I'm not sure how to figure out whether a thread has finished waiting because it was notified or because waiting timed out. Alternatively, I'd be interested to hear how anyone else has implemented lock timeouts?

Cheers,
Dan
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Murphy:

I would like to improve this so that a thread doesn't wait() forever for a record to be unlocked. I'm considering using wait(long timeout) instead, but I'm not sure how to figure out whether a thread has finished waiting because it was notified or because waiting timed out. Alternatively, I'd be interested to hear how anyone else has implemented lock timeouts?

Hi Dan,

You probably want your client to be woken up when the record it is trying to lock is unlocked. So if you test (after being woken up) if this is the case, you can easily determine why it was woken up: if the record is available, you can grab the lock; if it is not, it was probably woken up because of the time out.

Two more notes:
1. after your thread has been woken up, you will probably want to test if the record still exists; it might have been deleted by the previous lock owner.
2. Instead of Vector you may want to consider using ArrayList, which is basically the same thing, minus built-in synchronization, which gives a performance overhead and could cause unforeseen deadlocks if you were not aware of the built-in synchronization.

Frans.
 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frans,

Thanks for your response - very helpful. I'm interested in your comments about ArrayList v Vector. I didn't realise synchronisation could make deadlock more likely - can you explain how (or send me a URL which explains how)?


Thanks Again,
Dan
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Murphy:
Thanks for your response - very helpful. I'm interested in your comments about ArrayList v Vector. I didn't realise synchronisation could make deadlock more likely - can you explain how (or send me a URL which explains how)?

Hi Dan,

On second thought I think I jumped too quickly to conclusions on the deadlock issue. Since the Vector methods never call any other synchronized code, there is no deadlock risk. Sorry about that

But it is still good that you are aware that Vector's methods are synchronized; it might in some cases not be necessary for your code to synchronize on the Vector object.

Frans.
 
Michael Wiezik
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should use lockedRecords instead of LOCKED_RECORDS. I'm not sure, but I think you can loose some points using a coding style different than commonly accepted.
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michal Wiezik:
I think you should use lockedRecords instead of LOCKED_RECORDS. I'm not sure, but I think you can loose some points using a coding style different than commonly accepted.


It's common coding style to write static final members in all capitals. So this seems to be correct coding style.

Frans.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic