aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes isLocked (int recNo) is not used 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 "isLocked (int recNo) is not used" Watch "isLocked (int recNo) is not used" New topic
Author

isLocked (int recNo) is not used

Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 734
I defined code for isLocked(int recNo) method in Data.java as a "must" requirement. I just realize it is not used. I use another similar method , not in Data.java, to check if the record is locked instead.
Just like create/delete methods. I never use them even though I write code for them.

Will I fail because I don't use isLock() in Data.java?
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2264
    
    3

Himai Minh wrote:Will I fail because I don't use isLock() in Data.java?


Well, I think that it will depend if your locking mechanism works properly. If it doesn't, then you'll fail. I myself used the [tt]Data.isLocked(int)[/ii] method in my class that exposes the business methods remotely.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

Himai Minh wrote:Will I fail because I don't use isLock() in Data.java?

No. Just like you won't fail because you didn't use create/delete methods. But you have of course to make sure your locking mechanism works flawless and your code may not result in a deadlock at all.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Robin van Riel
Greenhorn

Joined: May 04, 2012
Posts: 20

About this getting the locking mechanism to work flawlessly, I'm still in the process of tackling this.

I've synchronized the relevant methods within the Data class and am currently using the following construction for the read, update, book and delete methods:

while rec x is locked => wait
- try
-- lock rec x
-- perform action on rec x
- finally
-- unlock rec x
-- notifyAll

The test class I'm using seems to be happy, but since I hardly ever use locking during real life projects, I hope I didn't overlook anything.
Thanks in advance for any feedback!
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

I guess you are completely on the wrong path. The lock-method should decide if a record can be locked or not (and if it's already locked, let the thread wait). The update/delete methods should just perform a check to see if the record is successfully locked (by the thread/client that's executing the update/delete request). The read-method just has to read the record, no locking provided. My Data class doesn't have a book-method (the book-method is a business service method, which is nothing more than a call to the update-method). And finally the unlock-method should release the lock on a given record and notify all waiting threads. But maybe I have misunderstood your explanation...
Robin van Riel
Greenhorn

Joined: May 04, 2012
Posts: 20

Thanks for your fast reply Roel!

You were right, I was on the wrong path.
My main mistake was trying to implement the locking mechanism on a higher abstraction level, while I should have kept it as close to the file manipulation classes as possible.

I'm still doing something wrong (test class fails), but those remaining bugs will be squashed .
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 734
Robin Van Riel buddy,

I suggest this logic for your lock(int rec) method:
-- try
-- lock
-- while (isLocked(rec X){
current thread waits
}

-- perform action on rec x
-- finally
-- unlock
For your unlock (int rec) method:
-- notifyAll waiting threads

Read Roberto's test, he used this logic:
data.lock(1)
data.update(...)
data.unlock(1);

I recommend you to read the Java API about lock. There is a good example of lock and unlock.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: isLocked (int recNo) is not used