Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My Locking Method: please, advise me.

 
james bonds
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
james bonds
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my spec it time out even forbiden: "consuming no CPU cycles until the desired resource becomes available"
 
james bonds
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!)
 
Maciej Miklas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11865
194
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic