java设置属性的取值范围是多少_jvm-Java系统属性的范围
系統(tǒng)屬性的范圍
至少?gòu)拈喿xProperties方法的API規(guī)范后,我無(wú)法獲得關(guān)于是否由JVM的所有實(shí)例共享系統(tǒng)屬性的答案。
為了找出答案,我編寫(xiě)了兩個(gè)快速程序,這些程序?qū)⑹褂孟嗤逆I但不同的值通過(guò)Properties設(shè)置系統(tǒng)屬性:
class T1 {
public static void main(String[] s) {
System.setProperty("dummy.property", "42");
// Keep printing value of "dummy.property" forever.
while (true) {
System.out.println(System.getProperty("dummy.property"));
try {
Thread.sleep(500);
} catch (Exception e) {}
}
}
}
class T2 {
public static void main(String[] s) {
System.setProperty("dummy.property", "52");
// Keep printing value of "dummy.property" forever.
while (true) {
System.out.println(System.getProperty("dummy.property"));
try {
Thread.sleep(500);
} catch (Exception e) {}
}
}
}
(請(qǐng)注意,運(yùn)行上面的兩個(gè)程序?qū)⑹顾鼈兿萑霟o(wú)限循環(huán)!)
事實(shí)證明,使用兩個(gè)單獨(dú)的Properties進(jìn)程運(yùn)行兩個(gè)程序時(shí),在一個(gè)JVM進(jìn)程中設(shè)置的屬性值不會(huì)影響另一個(gè)JVM進(jìn)程的值。
我應(yīng)該補(bǔ)充一點(diǎn),這是使用Sun的JRE 1.6.0_12的結(jié)果,并且至少在API規(guī)范中沒(méi)有定義此行為(或者我找不到它),行為可能會(huì)有所不同。
是否有任何工具可以監(jiān)視運(yùn)行時(shí)更改
據(jù)我所知。 但是,如果確實(shí)需要檢查系統(tǒng)屬性是否有更改,則可以一次保存Properties的副本,并將其與另一個(gè)對(duì)System.getProperties的調(diào)用進(jìn)行比較-畢竟Properties是Properties的子類(lèi), 因此比較將以類(lèi)似方式執(zhí)行。
下面的程序演示了一種檢查系統(tǒng)屬性是否已更改的方法。 可能不是一個(gè)優(yōu)雅的方法,但是它似乎可以完成它的工作:
import java.util.*;
class CheckChanges {
private static boolean isDifferent(Properties p1, Properties p2) {
Set> p1EntrySet = p1.entrySet();
Set> p2EntrySet = p2.entrySet();
// Check that the key/value pairs are the same in the entry sets
// obtained from the two Properties.
// If there is an difference, return true.
for (Map.Entry e : p1EntrySet) {
if (!p2EntrySet.contains(e))
return true;
}
for (Map.Entry e : p2EntrySet) {
if (!p1EntrySet.contains(e))
return true;
}
return false;
}
public static void main(String[] s)
{
// System properties prior to modification.
Properties p = (Properties)System.getProperties().clone();
// Modification of system properties.
System.setProperty("dummy.property", "42");
// See if there was modification. The output is "false"
System.out.println(isDifferent(p, System.getProperties()));
}
}
屬性不是線程安全的嗎?
Properties是線程安全的,因此我期望Properties也是這樣,事實(shí)上,Properties類(lèi)的API規(guī)范證實(shí)了這一點(diǎn):
此類(lèi)是線程安全的: 線程可以共享一個(gè)Properties 不需要外部物體 同步。,序列化表格
總結(jié)
以上是生活随笔為你收集整理的java设置属性的取值范围是多少_jvm-Java系统属性的范围的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 民生银行信用卡激活有效期是多久?99%的
- 下一篇: java信息管理系统总结_java实现科