aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes lock and wait 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 "lock and wait " Watch "lock and wait " New topic
Author

lock and wait

pramod karnani
Ranch Hand

Joined: May 22, 2007
Posts: 88
I implemented lock method like:




I have question that when lock method is called on already locked record then it has to wait . then it will block the code in while loop , is it okay to block the code ?

let say from test class I am doing like this :

lock(1);
lock(1);
here 1 is record number . so code will block for second lock statement.

I do not have cookie in my lock method .
public void lock(int recNo) throws RecordNotFoundException;

I am using Data class instance which implements the DBMain (Sun interface) to identify the Lock owner in lock /unlock/islock methods , is it okay ?

please reply .

thanks
pramod

[Edit: put code in UBB code tags]
Fola Fadairo
Ranch Hand

Joined: Feb 16, 2004
Posts: 35
It depends on what is in your specs. Offhand, I would say you are missing two things:

1.) You will have to catch InterruptedException.
2.) Call notify or notifyAll before leaving the lock method.



Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11523
    
100

Please don't shout out your name - it is quite distracting and makes me want to skip your post.

pramod KARNANI wrote:I have question that when lock method is called on already locked record then it has to wait . then it will block the code in while loop , is it okay to block the code ?

You will only be blocking a single thread, not blocking any other thread that is attempting to run that same bit of code. Sounds reasonable to me.

pramod KARNANI wrote:
lockedRecords.put(recNo, cookie);

...

I do not have cookie in my lock method .

...

I am using Data class instance which implements the DBMain (Sun interface) to identify the Lock owner in lock /unlock/islock methods , is it okay ?

You have said that you do not have a cookie in your lock method and that you are using the instance of the Data class to identify the lock owner. Cool. But why does your code have a variable named cookie?

How are you ensuring that each connected client gets a unique instance of the Data class and that they continue to use this unique instance throughout their interactions with the server?

Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11523
    
100

Fola Fadairo wrote:
2.) Call notify or notifyAll before leaving the lock method.

Why?

Andrew
pramod karnani
Ranch Hand

Joined: May 22, 2007
Posts: 88
thanks Andrew.
But we should call notifyAll in unlock method to signal the thread that lock is released
correct ?

Appreciate you .
I changed my profile also.

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5596
    
  15

pramod karnani wrote:
But we should call notifyAll in unlock method to signal the thread that lock is released
correct ?


Yes.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: lock and wait