java设置属性的取值范围是多少_jvm-Java系统属性的范围
系統屬性的范圍
至少從閱讀Properties方法的API規范后,我無法獲得關于是否由JVM的所有實例共享系統屬性的答案。
為了找出答案,我編寫了兩個快速程序,這些程序將使用相同的鍵但不同的值通過Properties設置系統屬性:
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) {}
}
}
}
(請注意,運行上面的兩個程序將使它們陷入無限循環!)
事實證明,使用兩個單獨的Properties進程運行兩個程序時,在一個JVM進程中設置的屬性值不會影響另一個JVM進程的值。
我應該補充一點,這是使用Sun的JRE 1.6.0_12的結果,并且至少在API規范中沒有定義此行為(或者我找不到它),行為可能會有所不同。
是否有任何工具可以監視運行時更改
據我所知。 但是,如果確實需要檢查系統屬性是否有更改,則可以一次保存Properties的副本,并將其與另一個對System.getProperties的調用進行比較-畢竟Properties是Properties的子類, 因此比較將以類似方式執行。
下面的程序演示了一種檢查系統屬性是否已更改的方法。 可能不是一個優雅的方法,但是它似乎可以完成它的工作:
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也是這樣,事實上,Properties類的API規范證實了這一點:
此類是線程安全的: 線程可以共享一個Properties 不需要外部物體 同步。,序列化表格
總結
以上是生活随笔為你收集整理的java设置属性的取值范围是多少_jvm-Java系统属性的范围的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 民生银行信用卡激活有效期是多久?99%的
- 下一篇: 运营体系_用户运营系统论:解构复杂产品的