File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes CPU cicles in lock method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "CPU cicles in lock method" Watch "CPU cicles in lock method" New topic

CPU cicles in lock method

John Oconnor
Ranch Hand

Joined: Jan 12, 2011
Posts: 67

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?


Roel De Nijs

Joined: Jul 19, 2004
Posts: 8380

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.

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
Roberto Perillo

Joined: Dec 28, 2007
Posts: 2271

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().

Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
John Oconnor
Ranch Hand

Joined: Jan 12, 2011
Posts: 67

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!

I agree. Here's the link:
subject: CPU cicles in lock method
jQuery in Action, 3rd edition