All instances of Preferences
are always thread-safe across the threads of a single Java Virtual Machine (JVM). Because Preferences
can be shared across multiple JVMs, there are special methods that deal with synchronizing changes across virtual machines.
If you have an application which is supposed to run in a single instance only, then no external synchronization is required.
If you have an application which runs in multiple instances on a single system and therefore Preferences
access needs to be coordinated between the JVMs on the system, then the sync()
method of any Preferences
node may be used to ensure changes to the Preferences
node are visible to other JVMs on the system:
// Warning: don't use this if your application is intended
// to only run a single instance on a machine once
// (this is probably the case for most desktop applications)
try {
preferences.sync();
} catch (BackingStoreException e) {
// Deal with any errors while saving the preferences to the backing storage
e.printStackTrace();
}