Java util首选项大约每30秒访问一次磁盘
我们的应用程序使用java.util.prefs.Preferences类来存储一些用户数据字段。下面是我们的preferences类的代码片段。我们的首选项的存储工作正常,但是我们注意到,首选项大约每隔30秒左右继续进行磁盘访问。有没有办法在Preferences类中禁用这些后台磁盘访问?(.userRootModeFile.root大约每30秒更改一次)Java util首选项大约每30秒访问一次磁盘,java,preferences,Java,Preferences,我们的应用程序使用java.util.prefs.Preferences类来存储一些用户数据字段。下面是我们的preferences类的代码片段。我们的首选项的存储工作正常,但是我们注意到,首选项大约每隔30秒左右继续进行磁盘访问。有没有办法在Preferences类中禁用这些后台磁盘访问?(.userRootModeFile.root大约每30秒更改一次) 公共类NTAppreference{ private static Preferences prefs=Preferences.userR
公共类NTAppreference{
private static Preferences prefs=Preferences.userRoot();//.systemRoot();
/**在“NTAP.applicationName.”前面加上“key”以实现系统范围的唯一性*/
私有静态字符串getKeyForApp(字符串键){
返回“NTAP.+Application.getApplicationName()+”+键;
}
/**
*返回“键”的应用程序首选项值。
*
*如果“key”不是已定义的首选项,则返回“def”。
*/
公共静态字符串get(字符串键、字符串定义){
返回prefs.get(getKeyForApp(key),def);
}
/**将“键”的应用程序首选项值设置为“值”*/
公共静态void put(字符串键、字符串值){
//TODO我们希望如何解决将数据放入首选项的故障?
试一试{
prefs.put(getKeyForApp(键),value);
prefs.flush();
}捕获(NullPointerException e){
LOG.error(NtapPreferences.class,e);
}捕获(IllegalArgumentException e){
LOG.error(NtapPreferences.class,e);
}捕获(非法状态){
LOG.error(NtapPreferences.class,e);
}捕获(备份存储例外){
LOG.error(NtapPreferences.class,e);
}
}
/**删除“键”的应用程序首选项值(如果已定义)*/
公共静态无效删除(字符串键){
prefs.remove(getKeyForApp(key));
}
}此行为由名为“java.util.prefs.syncInterval”的系统属性控制。这将以整数(
int
)值字符串的形式给出两次同步之间的间隔(秒)。您不能完全关闭同步,但可以将间隔设置为非常大的值。。。虽然有一个上限(我/我们认为)597天,但由于代码将间隔转换为毫秒的方式
参考:
MAX_INT-1
:-)MAX_INT-1不起作用。该期限为1.5至2年。(MAX_INT/1000/60/60)
为596
。因此,我的下一个猜测是,实现将syncInterval
转换为int
字段中的毫秒。在596到597天之间会出现整数溢出。或者,您也可以通过阅读代码来了解:-)
public class NtapPreferences {
private static Preferences prefs = Preferences.userRoot(); //.systemRoot();
/** Prepends "NTAP.<applicationName>." to 'key' for system-wide uniqueness. */
private static String getKeyForApp ( String key ) {
return "NTAP." + Application.getApplicationName() + "." + key;
}
/**
* Returns the application preference value for 'key'.<br/>
* <br/>
* If 'key' is not a defined preference, then 'def' is returned.
*/
public static String get ( String key, String def ) {
return prefs.get(getKeyForApp(key), def);
}
/** Sets the application preference value for 'key' as 'value'. */
public static void put ( String key, String value ) {
//TODO how do we want to resolve failures to put data in the preferences?
try {
prefs.put(getKeyForApp(key), value);
prefs.flush();
} catch (NullPointerException e) {
LOG.error(NtapPreferences.class, e);
} catch (IllegalArgumentException e) {
LOG.error(NtapPreferences.class, e);
} catch (IllegalStateException e) {
LOG.error(NtapPreferences.class, e);
} catch (BackingStoreException e) {
LOG.error(NtapPreferences.class, e);
}
}
/** Removes the application preference value for 'key' if one is defined. */
public static void remove ( String key ) {
prefs.remove(getKeyForApp(key));
}