That's against a must requirement, so that means automatic failure!
Actually, since I'm using master-lock phenomenon, once thread A obtains lock on record 'x', thread B will wait till thread A finishes with unlock. Then thread B will go for requesting lock on record 'x':
master_lock.lock
if(record 'x' not locked) {
x.lock
do stuff here
x.unlock
}
master_lock.unlock
Thus, thread B is anyway waiting till thread A finishes (and there won't be any message display like 'sorry you cannot get lock' etc.). So I guess I'm complying with must requirement, right?
I thought of using 'await' in lock method and 'signal' in unlock method (I'll still implement it anyway, so that, direct call to 'lock' outside synchronized context would wait till the record is available for locking). But now using both await/signal and master_lock is confusing me.
Because, anyway, only one thread is gonna get lock and 'do stuff' at a time - thanks to master lock, and thus there's no need for await/signal calls in locking methods.
And if I use await/signal, then there won't be any need of master_lock (when thread A gets the lock on record x, thread B will wait till A releases lock). Right? (please let me know if I'm right here)
So, what should I do?
Roel, as per your reply, I understand that:
1) If I make calls to await/signal via my locking mechanism, I would be complying to requirements, and there won't be automatic failure.
2) Further, if I go for master_lock (i.e. a static lock), the actual calls to await/signal will hold no significance ('condition' in while loop will never be satisfied, and await won't be called. further, there won't be anyone waiting for 'signal' so it is useless there). But it will provide an extra level of thread safety (and I guess there won't be automatic failure for this case either).
So, my current approach is to have both : await/signal and master_lock. Please advice.
Also, I read about someone who failed because he did not call wait and notify method from lock method in Data class.
What I've done is:
1) Create another class LockManager which contains reserve/release methods
2) Data class contains lock and unlock
3) lock invokes reserve and unlock invokes release
4) (now) reserve will call await and release will call notify
Is this approach valid? I guess so (since I'm calling await and signal - also I guess calling those methods instead of wait/notify is fine - since those methods are equivalent of each other). Please suggest.
Also, I'm allowing one client to lock only one record (to avoid deadlock). For this, I'm having a private boolean variable. When client gets a lock, variable is set to true.
What should happen if same client tries to lock another record? Is it fine if I throw an exception (which is child of RecordNotFoundException) and catch it at one level above 'lock' method? I'm really confuse over this. Does requirements expect me to wait here as well? I'm trying to avoid this (more locks by one client) situation because I can run into deadlock very quickly here. Please suggest.
And if client tries to lock same record for second time? Should I show message or not giving 'another' lock and pretend that nothing happened?
(Due to master_lock, those conditions are theoretically impossible, but anyway I would like to know)
I know I'm asking too much, but this(locking) is the most dangerous area of certification (and that is why it carries 80 points).
Roel, thanks a lot for making me aware of this big mistake (i.e. I must not show 'no lock for you' kind of messages).