| Author |
Locking assumption + possible typo?
|
John McParland
Ranch Hand
Joined: May 11, 2009
Posts: 83
|
|
Hi all,
I've been working on my Data class and have two queries;
1) Is it ok to assume that the thread which locks a record will also be the thread that unlocks it?
I've got tests which work when the same thread locks a record then releases it but cannot get a test working when one thread requests the lock and a separate thread releases it. In the latter case, I've got 6 threads requesting 3 locks, and 6 releasing those locks. Occasionally, my LockManager throws a Security Exception (not java.lang, but my own) saying the wrong the given cookie hasn't got the lock I'm trying to release.
2) In my problem description I have the following line;
"2 byte flag. 00 implies valid record, 0x8000 implies deleted record"
should the 0x8000 be 0x800? 0x8000 is 32768 while 0x800 is 2048. In Java a short (2 byte number!) can be up-to 32767 (2^15 -1) so 0x8000 cannot be read into 2 bytes! I think 0x800 is the way to go since I've got the data file read in using that.
Have I completely missed something here?
Regards,
John
|
Passed: SCJP 6 (90%), SCJD 6
|
 |
Roel De Nijs
Bartender
Joined: Jul 19, 2004
Posts: 3820
|
|
Hi John,
1/ If you have an interface with a lockCookie, it's easy: the record can only be unlocked with the same lockCookie, generated when locking the record. Your assumption "the thread which locks a record will also be the thread that unlocks it" is wrong, because if you use RMI you will not have that guarantee.
2/ It would be really weird if it is a typo, because these assignments are used for a really long time I think this thread contains valuable information for you.
Kind regards,
Roel
|
SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
|
 |
John McParland
Ranch Hand
Joined: May 11, 2009
Posts: 83
|
|
Hi,
thanks again for the prompt reply. Re;
1) The problem was in my tests, I wasn't sleeping in the releaser threads between attempts to get the cookie from the requester threads. This and simplifying how I set up the test fixed it.
2) Ah I tried some other methods of RandomAccessFile out from what I was already using and it works! A little more in-depth reading of the API was required.
Thanks again,
John
|
 |
Roel De Nijs
Bartender
Joined: Jul 19, 2004
Posts: 3820
|
|
Glad I could help and to hear you were able to fix your issues
Have a nice weekend!
Kind regards,
Roel
|
 |
 |
|
|
subject: Locking assumption + possible typo?
|
|
|