File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird lockRecord()" Watch "URLyBird lockRecord()" New topic
Author

URLyBird lockRecord()

Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 1048
    
  17

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 .


Formely Pawel Pawlowicz
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

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/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 1048
    
  17

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
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

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
Bartender

Joined: Jul 19, 2004
Posts: 5596
    
  15

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
http://www.javaroe.be/
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 812
    
    1
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: URLyBird lockRecord()