aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes My Locking Method: please, advise me. 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 "My Locking Method: please, advise me." Watch "My Locking Method: please, advise me." New topic
Author

My Locking Method: please, advise me.

james bonds
Greenhorn

Joined: Feb 19, 2007
Posts: 9
please, advise me about my Locking threadsafe.
I would appreciate any advise.

I implemented separate LockManager class using Singleton pattern.

And I used time out for escaping stale locks. The client can crashes(or is just shut down) sometime after requesting a logical record lock but before releasing the logical record lock.
In such a case, the record will be locked for all time - no other client will ever be able to lock the record.
There are many possible solutions for bypassing the problem.
Time out is one of them. It is relatively easier than the others.


I didn't use a HashMap, rather, I used a synchronized HashSet to track the locked records.
Because it does not need the cookie to track the owners of the locks.
The assignment does not require the cookie as the client identification.




[ February 21, 2007: Message edited by: james bonds ]
Khaled Mahmoud
Ranch Hand

Joined: Jul 15, 2006
Posts: 361
Hello,

I have the following comments about your solution and your code

1- Regarding the lock cookie,all the assignments I know about
the lock cookie is required.I also did Bodgitt and Scraper and there is a
lock cookie requirement.

2- Regarding releasing records that have been locked for so long time,
in my opinion, this adds more complexity to the system and will cause
many sort of problems.
Imagine the following scenario : Client A locks a record. Client A wants to update the record.He clicks on the update button on the GUI causing a new thread to compete for the locks object.This thread may sleep say 5 minutes.
In those five minutes, the component that will remove the locks on locked records will remove the lock on this record.Now thread was created by client
A wakes up and finds that record was unlocked.This will throw a security
exception.

3- As i understood from your code, is that you throw a run exception
if a thread waits more than 7 seconds.Actually a thread waiting for
a lock on a record can wait more than 7 seconds.

4- In your lock method,after the thread gets the lock on the
locks object again, you assume that the lock on that record
is released. This is not the case.

Good luck


SCJP, SCJD,SCWCD,SCDJWS,SCEA 5 MCP-C#, MCP-ASP.NET - http://www.khaledinho.com/
Life is the biggest school
james bonds
Greenhorn

Joined: Feb 19, 2007
Posts: 9
1- Regarding the lock cookie,all the assignments I know about
the lock cookie is required.I also did Bodgitt and Scraper and there is a
lock cookie requirement.


Cookie doesn't exist in my assignment.(Urlybird 1.3.1 version)


[ February 21, 2007: Message edited by: james bonds ]
james bonds
Greenhorn

Joined: Feb 19, 2007
Posts: 9
3- As i understood from your code, is that you throw a run exception
if a thread waits more than 7 seconds.Actually a thread waiting for
a lock on a record can wait more than 7 seconds.

4- In your lock method,after the thread gets the lock on the
locks object again, you assume that the lock on that record
is released. This is not the case.


I have to remove the time-out function from lock method ?

How can I fix my locking method ?
I also think that Time-out is bad idea than Unreferenced or Weakreference.
But, I think that is nothing like as bad as removing it. Am i right ??

The locking method of Monkhouse book is just 5 second.


[ February 21, 2007: Message edited by: james bonds ]
Maciej Miklas
Ranch Hand

Joined: Feb 12, 2007
Posts: 61
Hi,
In my opinion example of your book does not have a time out. It weak every 5 seconds to see if the record is free. If not it goes sleep. I would not do that, specially that my specification says: �consuming no CPU cycles until the desired resource becomes available� and this is not the case.


Regards,
Maciej
Maciej Miklas
Ranch Hand

Joined: Feb 12, 2007
Posts: 61
in my spec it time out even forbiden: "consuming no CPU cycles until the desired resource becomes available"
james bonds
Greenhorn

Joined: Feb 19, 2007
Posts: 9
in my spec it time out even forbiden: "consuming no CPU cycles until the desired resource becomes available"


No,no,no,

The sentence is not "must" , rether It is "should".

No forbidden !!!

In generally,That meaning(consuming no CPU cycles )is story about "Unlock".
It is not saying that "You can not use Time-out" .
A stale lock nothing like unlocking target, and it should be removed from a HashMap or HashSet(to track the locked record).
I think that it is not Automatic Failure.

Of cource, I prepare myself for cutting the locking point(approximately 36 point?).

Thanks anyway, just I will satisfy myself for passing the exam. That is all.



[ February 23, 2007: Message edited by: james bonds ]
Vincent Li
Greenhorn

Joined: Jan 12, 2007
Posts: 22
Originally posted by james bonds:


Cookie doesn't exist in my assignment.(Urlybird 1.3.1 version)





Yes, but how do you identify the caller to "unlock()" is the original owner who called the "lock()" method?

The client could implement with calls to:
unlock(recNo);
lock(recNo);

To maximimize it's chance of ALWAYS locking the record for modification, in which case, completely defeating the purpose of your lock()/unlock() scheme.

-- Vince
SCJP(1.4), SCWCD(1.4), SCJD(almost there!)


Vince<br />SCJP(1.4), SCWCD(1.4), SCJD (5.0)
Maciej Miklas
Ranch Hand

Joined: Feb 12, 2007
Posts: 61
I am using the Lock and Condition. All threads for patricular row are waiting on one condition - so I always know, which thread is waiting on particular row - no need t wake up every xxx ms, I can wake up thread directly in unlock method

Still using time out to break wait operation (the whole method) might be risky, who knows how they are testing...
[ February 23, 2007: Message edited by: Maciej Miklas ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11279
    
  59

Just adding some late comments ...
Originally posted by James Bonds:
I also think that Time-out is bad idea than Unreferenced or Weakreference.
But, I think that is nothing like as bad as removing it. Am i right ??
Personally I think that Unreferenced and WeakReference are elegant solutions. I dislike time-outs within the assignment, as I don't believe the instructions allow for them.
Originally posted by James Bonds:
The locking method of Monkhouse book is just 5 second.
True, but as mentioned multiple times in the book, the sample instructions that we followed in the book are deliberately different from the instructions given by Sun. If we did not make our instructions different, we would be giving away a solution. Our instructions specifically stated that a lock should be held no more than 5 seconds.
Originally posted by Maciej Miklas
In my opinion example of your book does not have a time out. It weak every 5 seconds to see if the record is free. If not it goes sleep.
Unfortunately you didn't get to see the complete code - we do return false (yet another difference from the real assignments) if we fail to get a lock after 5 seconds.
Originally posted by Maciej Miklas
I would not do that, specially that my specification says: �consuming no CPU cycles until the desired resource becomes available� and this is not the case.
Good point.

As James notes, this is not a "must" condition, however it is worth considering.
Originally posted by Vincent Li
Yes, but how do you identify the caller to "unlock()" is the original owner who called the "lock()" method [without a cookie]?
That is part of the challenge of the assignments that do not have cookies. There are multiple solutions, including:
  • Implementing the entire booking method server side, which means you could use the thread to identify the client
  • Implementing a connection factory, so that each connection can be used to identify the client (which also opens up the Unreferenced capabilities of RMI)
  • Have some extra parameter in the methods the client uses to lock, update, unlock, ... This extra parameter could identify the client and can be passed through the thread name to the lock method (I don't know if anyone has actually submitted a design that uses this)
  • Use sockets
  • And so on.

    Regards, Andrew


    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: My Locking Method: please, advise me.
     
    Similar Threads
    NX Problem with Threads and Concurrent Access to Database
    NX: About data consistent
    Appealing grading results?
    My design, File locking, record locking,please help!
    double lock + wait logic?