My Locking Method: please, advise me.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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)
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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:
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 ??
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 James Bonds:
The locking method of Monkhouse book is just 5 second.
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
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.
Good point.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.
As James notes, this is not a "must" condition, however it is worth considering.
That is part of the challenge of the assignments that do not have cookies. There are multiple solutions, including: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]?
Regards, Andrew
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
| What are you doing in my house? Get 'em tiny ad! The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |











