Java系统属性的范围 [英] Scope of the Java System Properties

查看:119
本文介绍了Java系统属性的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我们使用System.setProperty()方法来设置一些系统属性。根据这篇文章,使用系统属性有点棘手。

In Java we use System.setProperty() method to set some system properties. According to this article the use of system properties is bit tricky.


System.setProperty()可能是一个邪恶的调用。

System.setProperty() can be an evil call.


  • 100%线程充满敌意

  • 它包含超全局变量

  • 当这些变量神奇地
    在运行时发生变化时,调试非常困难。

我的问题如下。


  1. 系统属性的范围如何?它们是否特定于每个虚拟机,或者它们具有超级全局特性,它在每个虚拟机实例上共享相同的属性集?我想选项1

  1. How about the scope of the system properties? Are they specific for each and every Virtual Machine or they have a "Super Global nature" that shares the same set of properties over Each and every virtual machine instance? I guess the option 1

是否有任何工具可用于监控运行时更改以检测系统属性中的更改。 (仅为了便于检测问题)

Are there any tools that can be used to monitor the runtime changes for detect the changes in System properties. (Just for the ease of problem detection)


推荐答案

范围系统属性

至少从阅读 System.setProperties 方法,我无法得到系统属性是否由JVM的所有实例共享的答案。

At least from reading the API Specifications for the System.setProperties method, I was unable to get an answer whether the system properties are shared by all instances of the JVM or not.

为了找到答案,我写了两个快速程序,使用相同的键,但使用相同的键,通过 System.setProperty 设置系统属性:

In order to find out, I wrote two quick programs that will set the system property via System.setProperty, using the same key, but different values:

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

(请注意,运行上面的两个程序将使它们进入无限循环!)

(Beware that running the two programs above will make them go into an infinite loop!)

事实证明,当使用两个程序运行两个程序时单独的 java 进程,在一个JVM进程中设置的属性值不会影响其他JVM进程的值。

It turns out, when running the two programs using two separate java processes, the value for the property set in one JVM process does not affect the value of the other JVM process.

我应该补充一点,这是使用Sun的JRE 1.6.0_12的结果,并且至少在API规范中没有定义此行为(或者我无法找到它),行为可能会有所不同。

I should add that this is the results for using Sun's JRE 1.6.0_12, and this behavior isn't defined at least in the API specifications (or I haven't been able to find it), the behaviors may vary.

是否有任何工具可以监控运行时更改

据我所知。但是,如果确实需要检查系统属性是否有更改,可以同时保留属性的副本,并将其与另一个调用进行比较 System.getProperties - 毕竟, 属性 Hashtable ,因此将以类似的方式进行比较。

Not to my knowledge. However, if one does need to check if there were changes to the system properties, one can hold onto a copy of the Properties at one time, and compare it with another call to System.getProperties -- after all, Properties is a subclass of Hashtable, so comparison would be performed in a similar manner.

以下是一个程序,它演示了一种检查系统属性是否有变化的方法。可能不是一种优雅的方法,但它似乎可以完成它的工作:

Following is a program that demonstrates a way to check if there has been changes to the system properties. Probably not an elegant method, but it seems to do its job:

import java.util.*;

class CheckChanges {

  private static boolean isDifferent(Properties p1, Properties p2) {
    Set<Map.Entry<Object, Object>> p1EntrySet = p1.entrySet();
    Set<Map.Entry<Object, Object>> 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<Object, Object> e : p1EntrySet) {
      if (!p2EntrySet.contains(e))
        return true;
    }
    for (Map.Entry<Object, Object> 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()));
  }
}

属性是不是线程安全的?

Hashtable 是线程安全的,所以我期待属性也是如此,事实上, 属性 类确认:

Hashtable is thread-safe, so I was expecting that Properties would be as well, and in fact, the API Specifications for the Properties class confirms it:


这class是线程安全的:多个
线程可以共享一个属性
对象而无需外部
同步。,序列化表格

这篇关于Java系统属性的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆