gpt4 book ai didi

Java静态数组列表初始化问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:29:41 25 4
gpt4 key购买 nike

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

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

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 选项

--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

最佳答案

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

关于Java静态数组列表初始化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32310707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com