• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

B&S: locking etc

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,

Helen
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Helen Wallace
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chris,

Many thanks. Any idea about question 2?

Helen
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Rinke
 
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Helen,

Can I clarify if you have another book method that calls data.lock()->data.read()->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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic