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.