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 B&S: locking etc 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 "B&S: locking etc" Watch "B&S: locking etc" New topic

B&S: locking etc

Helen Wallace

Joined: Jul 02, 2007
Posts: 7
Dear friends,

I hope you all fine. I am new to this site and would like to ask some questions about locking that I am having problems with and wonder if you would be able to help me. My update() method in my Data class looks like this:

Now, io is the class which contains my random access file which I use to read and write to the database file with.

My questions are these:

1. Monkhouse says that we should use while loops inside synchronized blocks instead of if statements. Hence, I have used "while (isLocked(recNo)) {". The first question is: is this right?

2. My second question involves booking records. When the user books a record I have some code that looks like this:

... lock(); ... read(); ... update(); ... unlock(); ...

In other words I lock the record in question, read the latest version of the data from the database, update database with customer id and then unlock. Here are the issues:

a) Do I need to lock() before I read() or can I lock() after read() but before update().

b) My most serious problem is this. Since I call unlock() before I call update() when I reach the while loop in update() isLocked() returns true (as it would) which means I never progress further in the method than the while loop. What should one do in this case? Should I not call lock() and unlock() in the code that deals with the user selecting a record to book or should I not call lock() and unlock() in my update() method or is there something else that I am doing wrong? The reason I wanted to call lock() and unlock() in my calling code is because I would like to 'reserve' the record early on so that in my calling code if I get swapped out by some other thread they don't book my record under my feet. I'd be interested in knowing what you all have done.

Warm regards,

Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 443

The while is necessary its due to something called 'spurious thread wake up' which is very rare but basically your wait can return without you calling notify i.e. the JVM just returns from wait (seemingly randomly) in which case you wouldn't have passed the if statement i.e. had the lock ... but the while statement catches this and just does another wait.

"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Helen Wallace

Joined: Jul 02, 2007
Posts: 7
Hi Chris,

Many thanks. Any idea about question 2?

rinke hoekstra
Ranch Hand

Joined: Apr 06, 2007
Posts: 152
Hi Helen.

First of all: I don't have B&S, so my answer is for what it's worth. I have another assignment, so probably a completely different approach.

Then some remarks/questions:

1) why don't you put that while loop inside your lock method? Wouldn't that be more logical?

2) are you swallowing those exceptions, or did you just leave out the handling for the sake of not posting too much code details? Swallowing exceptions will almost certainly cost you points.

3) Why are you catching the RecordNotFoundException anyway? The method is saying that it throws this exception, so you might better not catch it.

Then, on your question about the read (2a): YES, you certainly have to put the read INSIDE the locked code. Otherwise another thread might jump in and do horrible things with the record you just read.

I'm afraid I can't follow you regarding question 2b. What do you mean:

Since I call unlock() before I call update() when I reach the while loop in update() isLocked() returns true (as it would)

You're not calling unlock before update, do you? I don't get the point here, sorry.


_ _ ________________________ _ _ <br /> <br />Just SCJP (but 93%)
Mark Smyth
Ranch Hand

Joined: Feb 04, 2004
Posts: 288
Hi Helen,

Can I clarify if you have another book method that calls data.lock()->>data.update()->data.unlock()in that order?

If so then when your thread gets to the islocked() while loop in your update method it will get stuck there infinately. There is no need to call the lock and unlock methods in the update method as these are in the public interface of the Data class and are meant to be used by external clients.

A thread should never be in the update method without having first locked a record. It probably be better to check that the record that the thread is trying to update has been locked and throw an Exception if this is not the case.

I agree. Here's the link:
subject: B&S: locking etc
It's not a secret anymore!