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  - synchronizing on the find method 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  - synchronizing on the find method" Watch "URLyBird  - synchronizing on the find method" New topic

URLyBird - synchronizing on the find method

Jesse Jesse

Joined: Jun 04, 2004
Posts: 22

I am working on the URLyBird assignment. My interface contains the following two signatures

// Reads a record from the file. Returns an array where each
// element is a record value.
public String[] read(int recNo) throws RecordNotFoundException;

// Returns an array of record numbers that match the specified
// criteria. Field n in the database file is described by
// criteria[n]. A null value in criteria[n] matches any field
// value. A non-null value in criteria[n] matches any field
// value that begins with criteria[n]. (For example, "Fred"
// matches "Fred" or "Freddy".)
public int[] find(String[] criteria);

I use a locking mechanism that uses a static hashmap in my data class that contains the recordNo of a locked record and an object that is used to synchronize on the record. I then test if a record is locked by looking in the hashmap. I do this any time a write action is performed.

My question is should the read and find methods above also be made thread safe. For example lets say client a performs a search by calling find and his search returns a record with record number =1. However at the same time client b has this same record locked, should this record be returned to the client even though it is locked? Within my find method I create a new RandomAccessFile Object that I use to iterate through the all of the records checking if a record meets the specified search criteria until EOF is reached.



Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11776

Hi Mark,

Your code does need to be thread safe, but that does not necessarily imply that a client cannot search for, or find, a record while it is locked.

Depending on how you write your code, you may have multiple threads running the same or different methods simultaneously.

But the purpose of logical record locking (using the lock() and unlock() methods) is that it allows you to have other processing happening (such as validating your record) without causing blocking.

All you need to do is ensure that while an individual record is being updated, you do not allow any other thread to access the record. You can therefore reduce your blocking to a much smaller interval.

Regards, Andrew

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
I agree. Here's the link:
subject: URLyBird - synchronizing on the find method
It's not a secret anymore!