File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird lockRecord() 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 "URLyBird lockRecord()" Watch "URLyBird lockRecord()" New topic

URLyBird lockRecord()

Paweł Baczyński

Joined: Apr 18, 2013
Posts: 1581

Hello all .
I have recently used my voucher and obtained my assignment .
I'm dealing with my lockRedord method now.
It was causing deadlock in Roberto's test in the deleting-records thread.

I had it like this:

Now I have it like

Is this ok? I mean checking isDeleted(recNo) twice?
One might say "If it works, it's ok".
I just want to be sure .

OCPJP 6, 7, 8, OCMJD 6
K. Tsang

Joined: Sep 13, 2007
Posts: 3130

Actually why are you checking if recordNo is null? recordNo is a primitive. It's better to check if it's <=0 or greater than your available total record number. Once such conditions passed then you check if record is already deleted.

So it's not necessary to call isDeleted(recNo) twice.

K. Tsang JavaRanch SCJP5 SCJD OCPJP7 OCPWCD5 OCPBCD5 OCPWSD5 OCMJEA5 part 1 part 2/3
Paweł Baczyński

Joined: Apr 18, 2013
Posts: 1581

K. Tsang wrote:Actually why are you checking if recordNo is null? recordNo is a primitive.

recNo is a primitive but record is not.
I use HashMap for storing records. It gives me null when I try to find a record that do not exist.

How do I verify that a record exists is not relevant to my question ;).
K. Tsang

Joined: Sep 13, 2007
Posts: 3130

Ah right I misread the code.

Well it's depends on your requirements. If the record has a delete flag (but not yet actually deleted in the file) then yes you will need the isDeleted() in the first if.

Roel De Nijs

Joined: Jul 19, 2004
Posts: 8364

First of all I would never check seperately if the record is null. That's a check which should definitely be in the isDeleted-method. I guess the isDeleted-method checks the deleted flag to see if it's a valid record or not. I consider this bad design, because the deleted flag is part of the data structure representing a record in the database file, but the deleted flag says something about the state of the record, so it should not be part of the data structure itself. So in your map the value is null (a deleted record) or not null (a valid record). It can't be any simpler than that!

Secondly about the main question: would it not be sufficient to check for a valid record after the locked record is unlocked instead of checking once before and once after? I don't think there are huge benefits when checking twice (except saving a few CPU cycles).

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 1114
Hello Pawel,

In terms of the deadlock, my other concern is : is the unlock method notifying the waiting threads?
Make sure the waiting threads are waiting for the same lockedRecord object.
For example, one thread does this:

This is a scenario:
1. Thread 1 lock record 1
2. Thread 2 tries to lock record 1, but it waits for it.
3. Thread 1 marks the record as deleted in the database or memory.
3. Thread 1 unlock record 1 , check if record no has been reserved from the memory. If not , throw record not found exception.
4. Thread 1 notifyAll waiting threads, thread 2.
5. Thread 2 wakes up,it finds out the record is not locked anymore. It moves on to the delete method. But it finds out that the record is deleted in the delete method, throws the exception, which is caught.
6. When the exception is caught, you can put the unlock method in the finally method.
I agree. Here's the link:
subject: URLyBird lockRecord()
It's not a secret anymore!