• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FBNS: Question about Unreferenced Example by Mr. Monkhouse

 
Ian Wark
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.

This is going back a bit, but I came across an example of using Unreferenced by Andrew Monkhouse in a search on this forum.

The link is
http://www.coderanch.com/t/184332/java-developer-SCJD/certification/Rmi

What conclusion is one to draw from the example? When I ran the code I noticed that as soon as ClientDied finished Unreferenced was called.
But the thread that it spawns keeps going.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ian,

You might want to look at this thread, as it is a bit more explicit about what should happen and when it should happen.

You are correct about the conclusion you came to: that the remote method continues to run, even after the client disconnects.

Regards, Andrew
 
Ian Wark
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew,

I hope you enjoyed pleasant weather in the land of the rising sun.

About the example, I still do not understand why the unreferenced is running when it does. My understanding of unreferenced is that it is simply a lease, and when the client ceases to update its lease by making the necessary method call, RMI assumes that it is unreferenced. If ClientDied is still running its job, what is being unreferenced? This example seems like a warning saying.. hey don't rely on unreferenced. Or, don't create your own threads when using RMI. I don't get it.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ian,

I managed to get sunburnt in the land of the rising sun . Naturally, after enjoying 4 hot sunny days in Hiroshima, Miyajima, Osaka, and Kyoto I finally found a place that sold baseball caps (which I desperately needed at that stage), only to have 1.5 days of light rain in Tokyo But I thoroughly enjoyed what little I got to see of Japan in the short time I was there.

Unreferenced tells the instance of the class that there are no more remote references to that instance of the class. This has nothing to do with how many local threads are running on that instance of the class.

You have to write your code with this in mind. In all probability, you would want to do any clean up of granted locks, plus possibly release any locks that are granted after Unreferenced are called. Alternatively you might like to interrupt any waiting thread that is currently running on an unreferenced instance of a class (this can depend on how you handle interruptions in your locking code).

The powerful thing about Unreferenced is that it only provides notification that the client is disconnected - what you do from that point is up to you. Your business rules might have you complete a transaction, or roll it back, or .... It is all up to you.

[Just to confuse matters, by the way, it is possible for you to write server code which will result in a remote reference to your code being created. This would result in unreferenced being called multiple times I don't think I have ever posted an example of that in this forum, as the examples I have come up with have been extremely contrived . I cannot imagine anyone writing such code in the SCJD assignment.]

Regards, Andrew
 
Ian Wark
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew,

I think you had a fairly good run in Japan. The weather is pretty pleasant. Normally Japan is more like "the land of the scorching sweatbox".

I think the penny has dropped regarding the example. Thanks for that.

Getting to the granted locks issue, does unreferenced really help me with releasing unused locks? I could make a direct call from unreferenced to my locking collection to check that the key for the remote object is not in there (and remove if present).. but that feels rather clumsy. It seems that my collection would be too hard to access too. People who use unreferenced for this purpose are using it in another way (yes?). The idea of a WeakHashMap for releasing unused locks sounds better. Is the idea to have a WeakHashMap with references to the remote object as unique id? They need to be wrapped in WeakReference, right? Then have a daemon thread constantly looking for nulls as keys and then removing those. Yes? No need for unreferenced. Do I need unreferenced to promote garbage collection, so that old references turn into nulls quickly? I don't know.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic