• 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

lock and wait

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic