Thread-3 Enters Locking.
Thread-3 Trying to lock record number: 0
Thread-3 Places Cookie: 0 in Hashmap
Thread-3 Enters Locking.
The current thread either has this record locked or it is attempting to lock multiple records concurrently.
AWT-EventQueue-0 Enters Locking.
AWT-EventQueue-0 Trying to lock record number: 16
AWT-EventQueue-0 Places Cookie: 16 in Hashmap
AWT-EventQueue-0 Enters Unlocking.
AWT-EventQueue-0 Removes Cookie: 16 from hashmap.
AWT-EventQueue-0 Removes Itself from Thread Manager.
All threads notified.
SCJP 1.4<br />SCJD 1.4
Well here is what's happening. Before, I had my unlock in the try clause. It did this sequence lock-read-verify-update-unlock. However, if the verification proved to be that the record was already booked, the book exception was thrown, exiting the try statement (skipping the unlock) and leaving that record forever locked. I decided to put the unlock in finally because of that to make sure unlock would always be called. (Keep in mind, that if it calls lock and the record is booked, it will halt and wait for it.) Is there a better way in doing this? Also, I COULD change the sequence to read-lock-read-update-unlock, but that seems a bit repetitive, I think. Keep in mind that I just started the networking portion, so all the above is being tested in local (I'm using locking in local). Say a thread calls something like:Originally posted by Dieskun Koper:
Is there a point in calling the unlock method if a lock method did not return successfully?
SCJP 1.4<br />SCJD 1.4
Originally posted by Dieskun Koper:
How about the following:
long c = data.lock(recNo);
try {
// validate owner field and see if it is available to book, etc.
}
catch {...
}
finally {
data.unlock
}
Here, if your lock method throws an exception (i.e. definitely did not get a lock), it won't try to unlock it (which might cause another exception).
SCJP 1.4<br />SCJD 1.4
Originally posted by Dieskun Koper:
I don't know when your ThreadLockException gets thrown, but if the record does not get locked when it's thrown but you want to catch and wrap this exception, just add another try-catch around the try-catch-finally. If the record gets locked before this exception is thrown and it needs to be unlocked, you are stuck with an ugly design and only surgery can fix that.
SCJP 1.4<br />SCJD 1.4
ThreadLockException is thrown if a thread attempts to 1) lock a record
* that itself has already locked or 2)lock multiple records in the database
* while it is already holding a lock on a record.
SCJP 1.4<br />SCJD 1.4
Originally posted by Daniel Simpson:
P.S.- If anyone can think of a better name for my exception ThreadLockException, let me know. I don't think that name describes the exception very well and at first glance you would think it has to do with threads deadlocking. However, I am doing logical locking with the threads to make sure it can't (as my Javadoc for that exception describes):
Thanks!
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|