Java静态arraylist初始化问题

时间:2015-08-31 11:57:31

标签: java arraylist

这个问题只发生在我的生产中,我自己无法在测试/开发过程中重现它

我有一个包含很多静态变量的类。

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被初始化为空列表。

任何人都能说出这种罕见的情况会发生吗?这种情况在生产环境中不断发生,但无法在本地重现。

环境详情:

  • Redhat Enterprise 5.3服务器64位(更新U3)
  • JDK7
  • jvm options
  

- 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:+ CMSPermGenSweepingEnabled

1 个答案:

答案 0 :(得分:0)

代表@Marko Topolnik和@ 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"));