>> [...]
>> I think given the problem you are having that I wouldn't try to update
[quoted text clipped - 18 lines]
>
> Pete
Yes I'm agreeing that you have created doubt about whether it would
actually be safe.

Signature
Knute Johnson
email s/nospam/knute/
------->>>>>>http://www.NewsDem
>> [...]
>> I think given the problem you are having that I wouldn't try to update
[quoted text clipped - 4 lines]
> I could do the former, though it seems wasteful. Because of that I'd
> prefer to leave it until shutdown.
How wasteful? I think that you might be falling into the pitfall of
premature optimization. Unless you change the preferences thousands of
times a second, I wouldn't bother. I think its pretty typical to update
the preferences as the user makes there changes.
> As far as the latter goes, I can't do that because the run-time can be
> shut down without actually closing my window. I did try a listener to
> respond to the closing event, and never received it in shutdown
> scenarios that don't actually involve closing the window (e.g. on the
> Mac, using the "Quit" menu item provided by the run-time).
Not to mention that a JVM could actually die without running the
shutdown hooks at all.
> So, I infer from your reply that you feel it would actually be unsafe to
> use the Preferences class in a shutdown hook, even calling
> Preferences.flush()? Indeed, this was my concern, and it sounds like
> you are confirming it (or at least are agreeing that I should be worried
> :) ).
I looked at the WindowsPreferences version, and it doesn't use shutdown
hooks. That doesn't mean anything about the OSX version though.
I really think that you should make the changes when the state changes.
Leave the caching and flushing to the Preference class. If you find
performance problems, *then* you can optimize (with aid from a profiler
ofcourse).

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Peter Duniho - 05 Feb 2008 18:40 GMT
>>> 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
[quoted text clipped - 7 lines]
> times a second, I wouldn't bother. I think its pretty typical to update
> the preferences as the user makes there changes.
I don't disagree with that philosophy per se. It's one I recommend to
others on a regular basis. But the thing I'm saving is a (relatively)
unbounded collection of data objects. The user can add arbitrarily many
of them in the application, and I'm trying to save the entire collection.
It won't change thousands of times a second, but it could change several
times a second (from user input), and the data could be large enough for
this to cause a noticeable slowdown.
Now, a) for most users this collection is likely to be very small (in
fact, in many cases it may never contain anything), and b) there are other
approaches to saving the collection (such as not storing the entire
collection as a single preference object). So it's true that I don't
necessarily need to use this approach.
But doing it this way makes the code a lot simpler, and another philosophy
I recommend is that of simple code. It's harder to put bugs in simple
code. :)
> Not to mention that a JVM could actually die without running the
> shutdown hooks at all.
A premature exit is always a potential risk. This data isn't what I'd
call "mission critical" :), so I'm willing to live with the increased risk
due to saving data only at exit.
>> So, I infer from your reply that you feel it would actually be unsafe
>> to use the Preferences class in a shutdown hook, even calling
[quoted text clipped - 4 lines]
> I looked at the WindowsPreferences version, and it doesn't use shutdown
> hooks. That doesn't mean anything about the OSX version though.
I found a web page that purported to state the name of the person who
implemented the Preferences class (Joshua Bloch), and where code was
posted that supposedly came from that implementation. In the same code, a
shutdown hook _is_ used, but it's very simple. The code posted would
explain the behavior I saw, but suggests also that there's no harm at all
in using the Preferences class from a shutdown hook oneself, as long as
one does call flush().
In particular, the Preferences implementation in its shutdown hook only
disables the timer used to periodically flush the data to the OS storage,
and then actually does a flush. It doesn't otherwise tear down any
functionlity within the class (which would be my main concern). This is
the link: http://allaboutbalance.com/articles/disableprefs/
Thanks,
Pete