The moose likes Developer Certification (SCJD/OCMJD) and the fly likes forced  to lock and then read Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "forced  to lock and then read" Watch "forced  to lock and then read" New topic

forced to lock and then read

Bob Stone

Joined: Mar 28, 2007
Posts: 12
Hi Randchers,

I�ve finished my B&S coding, and now I am doing Data.class testing for updateRecord() and deleteRecord(), but found the lockRecord method is not thread�safe. Here are the codes:

where data is an instance of Data.class and bookManager is to logically lock (place an entry in a WeakHashMap) a record which prior to deleting or updating.
When multiple threads, delete and update together, acquire same record, a delete thread can lock a record that has been deleted. Say:
a delete thread and a update thread both can find the same record, now the update thread go asleep and the delete record carries on and finally removes the record. Afterward, a update record wakes up and then successfully locks up an unexisting record.

To fix it, an alternative is to lock record first regardless of it's existence. Here is the method

Is it odd to handle securityException exception by rethrow a RecordNotFoundException exception with "Cookie held by another..." ? Or other solutions can anyone draw my attention?

Thanks very much
Bob Stone

Joined: Mar 28, 2007
Posts: 12
I made mistake, the alternative should be :

I agree. Here's the link:
subject: forced to lock and then read
It's not a secret anymore!