• 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

CPU cicles in lock method

 
Ranch Hand
Posts: 67
Java ME Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi for all,

In my DB interface (and assignment) have the next indication:




I have made the next(suppose two clients try lock some record)

client A lock recNo 1;
client A return lock cookie;
client B try lock recNo 1;
because recNo 1 already is lock, for client B thows a SecurityException where indicate that is already lock.


I am ok?

Thanks
 
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
With such an approach you will automatically fail, because you are violating a must-requirement defined by the interface you have to implement!

clientB has to wait and when record 1 is unlocked by clientA, clientB will be notified and tries again to lock record 1.
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Oconnor wrote:I am ok?



Hum... no. Because, if record 1 is locked by client A, then when client B tries to lock it, it must not consume CPU cycles... with this approach of yours, it is consuming CPU cycles. In the lock method, while a record to be locked is already locked, then the client that is trying to lock it must wait().
 
John Oconnor
Ranch Hand
Posts: 67
Java ME Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roberto Perillo wrote:while a record to be locked is already locked, then the client that is trying to lock it must wait(). .



Ok, so I have only that call a wait(), while my records lock map contains the record number. In a syncronized context clear.


OKOKOK, I am wrong, but, now while i have re-view the code, I have worked with a ReentrantLock, for this reason, the first thread that take the lock, work fine until lock.unlock.
So allways if there are anothers theads try take the lock canĀ“t because already is lock, only when the current thread release the lock, another thread can execute the lock block code.

Now this approuch is like SCJD exam with J2SE by Monkhouse.

in my block code, is verify that record number is a valid record in database through read(recNo). but I trhow a securityException when record is locked with another lockCookie

this is the reason because I have throw a SE if already is lock some register for a second client.
So i have not consuming cpu cicles thanks to use lock.lock and lock.unlock (reentrantLock) and also i have validate that map contains a lock cookie for fist client that obtain the lock.

sorrry for my fist expose in post, but is good have a re-view for code, at this moment i have re-view all!

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic