你不需要同步,System.setProperty
已經同步方法,雖然System.getProperty
不同步,屬性類擴展已經syncronized方法來訪問它的鍵值pairs.As結果哈希表,你不需要做的擔心同步問題..
編輯:
關於我的回答一些澄清;
1.如果您的方法應該工作作爲一個原子單元,它應被同步,如;
public static sychronized void someMethod(){
if (System.getProperty("responderURL")==null)
System.setProperty("responderURL",IP);
//do code here
//some code
URL url = new URL(System.getProperty("responderURL"),8080,"test.txt");
}
您應該同步您的方法,因爲雖然線程1正在檢查responderURL屬性,但線程2可以設置此屬性。
2.But如果你的方法設置僅responderURL屬性,其功能並不依賴respondrURL財產電流值,你不需要同步方法..
public static void someMethod(){
System.setProperty("responderURL",IP);
//do code here
//some code
URL url = new URL(IP,8080,"test.txt");
}
像往常一樣有缺陷:過時讀取。系統屬性和任何共享變量之間沒有區別。 – bestsss 2011-05-17 11:23:05