[英]Java static arraylist initialization issue
This issue happens to me only in production and am myself not able to reproduce it during testing/development 这个问题只在生产中发生,我本人在测试/开发过程中无法重现
I have a class containing lot of static variables. 我有一个包含很多静态变量的类。
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);
}
}
When I access MY_MAP.get("test1")
or MY_MAP.get("test2")
or for that matter on any key on MY_MAP
, I always get empty list. 当我访问MY_MAP.get("test1")
或MY_MAP.get("test2")
或就MY_MAP
上的任何键进行MY_MAP
,总是得到空列表。 Looks like for some reason MY_LIST
is initialized to empty list. 看起来由于某种原因, MY_LIST
被初始化为空列表。
Can anyone tell in what rare case can this happen? 谁能在少数情况下发生这种情况? This is constantly happening in production environment but am not able to reproduce this locally. 这在生产环境中经常发生,但是无法在本地复制。
Environment details: 环境详细信息:
--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 --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
Posting on behalf of @Marko Topolnik and @marco13 It was what @Marko Topolnik suggested in one of his comments. 代表@Marko Topolnik和@ marco13张贴@Marko Topolnik在他的评论之一中建议的内容。 There was indeed a removeAll statement which was reachable only in production which was actually emptying the list. 确实有一个removeAll语句,该语句仅在生产环境中才可用,实际上正在清空列表。
As @marco13 suggested, this issue would have been identified if only we created unmodifiable lists at the first place. 正如@ marco13所建议的,只有首先我们创建了不可修改的列表,此问题才能被发现。 So to avoid bumping into such issues, the code could be modified like below 因此,为避免遇到此类问题,可以如下修改代码
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"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.