• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

lock and wait

 
pramod karnani
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fola Fadairo wrote:
2.) Call notify or notifyAll before leaving the lock method.

Why?

Andrew
 
pramod karnani
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10407
133
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pramod karnani wrote:
But we should call notifyAll in unlock method to signal the thread that lock is released
correct ?


Yes.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic