• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Weak references

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have implemented my locking and all appears to work but I am trying to get WeakReferences to work.

Currently I create a weak refernec for my clients and pass that to the lock manager, a record is locked and the weakref stored with it in a Hashmap. I then havbe anothetr client attempt to lock the record, it cant and so waits. I then terminate the original client. I was expecting the second client to come out of the wait, but how can that happen. What am I doing wrong as I must still have a valid reference to the weakRef of the first client. Any Ideas, should i be using a weakHashMap? (possibly not as no one has mentioned that!)

Thanks
Lee
 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't implement that but I believe the WeakHashMap is correct, give it a shot.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lee,

I am curious about the comment "Any Ideas, should i be using a weakHashMap? (possibly not as no one has mentioned that!)" What hasn't been mentioned? Doing a search for WeakHashMap in this forum returns 70 matches, and there are 20 matches for WeakReference.

There is also a really great post by Phil Maquet where he takes the kind of comments I make below, and creates a viable solution from it. Along the way he discards the WeakHashMap as he felt the WeakReference was in the wrong place (the key - he wanted it to be the value) so he created his own collection. Perhaps searching for his member number with either of the keywords might bring it up.

There are several things you need to be aware of:
  • Make sure your key is something that is unique to your client, and will therefore disappear when your client disapears.


  • Remember that the WeakReference will only be cleared if there are no more strong references to the key. A common mistake is to use the one and only remote object that you exported as the key - however this will always have a strong reference (held by the RMI registry) so it will never be removed from a WeakHashMap (and Unreferenced will never be called (for those who implement that interface)).

  • Make sure you wait long enough for the distributed garbage collector (DGC) to run.


  • As this is (presumably) an RMI solution, a distributed garbage collector will run roughly every 20 minutes. You can change the lease value to reduce the time before the DGC notices that there are no more strong references (i would recommend against permanently setting this - just set it from the command line for testing, and note it in your documentation), or you could have a separate thread requesting that garbage collection run more often (see next point).

  • Have some other thread running that will wake waiting threads if the DGC removes a key-value pair from the collection.


  • Just having the key-value pair removed from the collection is only half the solution, and it has a potential problem: waiting clients have not been notified that the lock has been removed. How can they? They are waiting for some thread to call notify (or notifyAll), and the WeakHashMap does not know to do this.

    So you are going to need a separate thread (possibly a daemon thread) that will watch the WeakHashMap and call notifyAll if a record has been removed from it. You might want an accessible counter that monitors how many records have been explicitly locked and compare that to how many records are in the WeakHashMap, and perform suitable actions if they differ.


    Hopefully this has given you some ideas.

    Regards, Andrew
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic