2015-08-31 51 views
1

這個問題發生在我身上只有在生產和是我自己無法測試/開發Java靜態數組列表初始化問題

期間重現它,我有一個包含大量靜態變量的類。

public class MyClass { 

private static final ArrayList<String> MY_LIST = new ArrayList<String>(Arrays.asList("DCNC", "DJPC", "DKRC", "DTHC", "DTWC", "DILC", "DALC", "DSEC", "DTRC", "DZAC", "DARC", "DCOC", "DRUC", "DMYC", "DMXC", "DPKC", "DIDC", "DBRC", "DNPC", "DJOC", "DLKC", "DVNC", "DKHC", "DLAC")); 
public static final HashMap<String, ArrayList<String>> MY_MAP = new HashMap<String, ArrayList<String>>(); 

static { 
    MY_MAP.put("test1", MY_LIST); 
    MY_MAP.put("test2", MY_LIST); 
    MY_MAP.put("test3", MY_LIST); 
    MY_MAP.put("test4", MY_LIST); 
    MY_MAP.put("test5", MY_LIST); 
    MY_MAP.put("test6", MY_LIST); 

} 

private static final ArrayList<String> MY_SECOND_LIST = new ArrayList<String>(Arrays.asList("DCNC", "DJPC", "DKRC", "DTHC", "DTWC", "DILC", "DALC", "DSEC", "DTRC", "DZAC", "DARC", "DCOC", "DRUC", "DMYC", "DMXC", "DPKC", "DIDC", "DBRC", "DNPC", "DJOC", "DLKC", "DVNC", "DKHC", "DLAC")); 
private static final ArrayList<String> MY_THIRD_LIST = new ArrayList<String>(Arrays.asList("DJPC", "DKRC", "DTHC", "DTWC", "DILC", "DALC", "DSEC", "DTRC", "DZAC", "DARC", "DCOC", "DRUC", "DMYC", "DMXC", "DPKC", "DIDC", "DBRC", "DNPC", "DJOC", "DLKC", "DVNC", "DKHC", "DLAC")); 
private static final ArrayList<String> MY_FOURTH_LIST = new ArrayList<String>(Arrays.asList("DCNC", "DKRC", "DTHC", "DTWC", "DILC", "DALC", "DSEC", "DTRC", "DZAC", "DARC", "DCOC", "DRUC", "DMYC", "DMXC", "DPKC", "DIDC", "DBRC", "DNPC", "DJOC", "DLKC", "DVNC", "DKHC", "DLAC")); 
private static final ArrayList<String> MY_FIFTH_LIST = new ArrayList<String>(Arrays.asList("DKRC", "DTHC", "DTWC", "DILC", "DALC", "DSEC", "DTRC", "DZAC", "DARC", "DCOC", "DRUC", "DMYC", "DMXC", "DPKC", "DIDC", "DBRC", "DNPC", "DJOC", "DLKC", "DVNC", "DKHC", "DLAC")); 

public static final HashMap<String, ArrayList<String>> MY_SECOND_MAP = new HashMap<String, ArrayList<String>>(); 

static { 
    MY_SECOND_MAP.put("T1", MY_SECOND_LIST); 
    MY_SECOND_MAP.put("T2", MY_THIRD_LIST); 
    MY_SECOND_MAP.put("T3", MY_FOURTH_LIST); 
    MY_SECOND_MAP.put("T4", MY_FIFTH_LIST); 
}  

} 

當我訪問MY_MAP.get("test1")MY_MAP.get("test2")或上MY_MAP任意鍵這個問題,我總是空單。看起來由於某種原因,MY_LIST被初始化爲空列表。

任何人都可以告訴在什麼情況下會發生這種情況?這種情況在生產環境中經常發生,但無法在本地重現。

環境細節:

  • 紅帽企業5.3服務器64位(更新U3)
  • JDK7
  • JVM選項

--FLApplication.machineProtocol = UDP -Dclient .encoding.override = UTF-8 -Dfile.encoding = UTF-8 -Xmx512m -Xms512m -XX:SurvivorRatio = 8 -XX:PermSize = 128M -XX:MaxPermSize = 128M -XX:+ CMSClassUnloadingEnabled -XX:+ CMS PermGenSweepingEnabled代表@Marko Topolnik的

+13

生產,認真嗎? –

+0

是............. –

+1

請指定環境:操作系統,Java版本,jvm選項。你使用多線程嗎? – sibnick

回答

0

發佈和@ marco13 這是什麼@Marko Topolnik在他的評論人建議。確實有一個removeAll語句只能在生產中實現,實際上是清空列表。

正如@ marco13所建議的那樣,如果只是我們在第一個地方創建了不可修改的列表,那麼就會發現這個問題。所以爲避免碰到這樣的問題,代碼可以修改如下:

 private static final List<String> MY_LIST = Collections.unmodifiableList(Arrays.asList("DCNC", "DJPC", "DKRC", "DTHC", "DTWC", "DILC", "DALC", "DSEC", "DTRC", "DZAC", "DARC", "DCOC", "DRUC", "DMYC", "DMXC", "DPKC", "DIDC", "DBRC", "DNPC", "DJOC", "DLKC", "DVNC", "DKHC", "DLAC"));