Re: Accessing Preferences from ShutdownHook?
Peter Duniho wrote:
I am trying to save some data during user shutdown. To this end, I've
added a ShutdownHook to run when the application shuts down
(Runtime.addShutdownHook()), and in the run() method for the Thread
instance added, I use the Preferences class to save data.
What I found is that unless I call Preferences.flush(), the data wasn't
actually saved. On the one hand, this doesn't seem like an unreasonable
requirement. On the other hand, the documentation _explicitly_ states:
Normal termination of the Java Virtual Machine
will not result in the loss of pending updates
-- an explicit flush invocation is not required
upon termination to ensure that pending updates
are made persistent.
Which to me seems to mean that as long as I don't care when the data is
saved, that it _will_ be saved.
Since it's definitely not being saved, this makes me wonder if it's
kosher to save data to the user preferences at all. On the one hand, I
did get it to work by explicitly flushing the preferences node, but on
the other hand the fact that this was necessary at all suggests that
maybe I shouldn't be trying to access the preferences from my shutdown
hook in the first place.
The documentation for Runtime.addShutdownHook() says, with respect to
the shutdown hook itself:
... They should also not rely blindly upon services
that may have registered their own shutdown hooks
and therefore may themselves in the process of
shutting down.
Since the Preferences class is documented as having at least some degree
of asynchronous behavior, it seems possibly (likely?) that it has its
own shutdown hooks, and thus is relevant to that quote.
At the very least, that would explain why in spite of the Preferences
class saying that you don't need to call flush(), that you actually do
in this case. After all, the Preferences class might already believe
it's done writing all pending preferences out by the time my own
shutdown hook tries to write a new one.
But more importantly, I'm concerned that I shouldn't be trying to write
new preferences data at all in my shutdown hook.
Can I safely do so? Is it simply a matter of needing to call flush(),
since I can't rely on the Preferences own shutdown behavior to write the
data? Or am I tempting fate here? And if so, is there somewhere in the
Java API documentation that covers this? I did look, but couldn't find
anything specific to this exact scenario.
Thanks!
Pete
I looked at the docs and I don't see anything that would cause it not to
work as advertised (except that you say it doesn't). Your assumption
that Preferences uses a shutdown hook to store it's own data and is
missing data you store in another hook is plausible. The following
quote is from the docs on Runtime.addShutdownHook();
"Shutdown hooks run at a delicate time in the life cycle of a virtual
machine and should therefore be coded defensively. They should, in
particular, be written to be thread-safe and to avoid deadlocks insofar
as possible. They should also not rely blindly upon services that may
have registered their own shutdown hooks and therefore may themselves in
the process of shutting down. Attempts to use other thread-based
services such as the AWT event-dispatch thread, for example, may lead to
deadlocks."
I think given the problem you are having that I wouldn't try to update
my Preferences in a shutdown hook, but instead do it earlier, either
when the state changes that you want to keep or before the actual
shutdown starts such as in a WindowListener.
--
Knute Johnson
email s/nospam/knute/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDem