Java util Preferences大约每30秒访问一次磁盘

时间:2013-06-29 02:46:06

标签: java preferences

我们的应用程序使用 java.util.prefs.Preferences 类来存储一些用户数据字段。以下是我们的偏好类的代码片段。存储我们的首选项工作正常,但我们注意到首选项大约每30秒左右继续进行磁盘访问。有没有办法在Preferences类中禁用这些后台磁盘访问? (.userRootModeFile.root大约每30秒更改一次)

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));
}

}

1 个答案:

答案 0 :(得分:4)

此行为由名为“java.util.prefs.syncInterval”的系统属性控制。这将同步之间的间隔(以秒为单位)作为整数(int)值字符串。你不能完全关闭同步,但你可以将间隔设置为一个非常大的值...虽然有一个上限(我/我们认为)597天,由于代码将间隔转换为毫秒的方式

参考: