public class SipResponseCollection {
private final static Logger LOGGER=Logger.getLogger(SipResponseCollection.class);
private volatile Map<String, List<SipResponse>> map = new HashMap<String, List<SipResponse>>();
public SipResponseCollection() {
}
public boolean contain(String callId, int statusCode) {
List<SipResponse> list = map.get(callId); //pop null exception in linux amd64 machine but right for windows machine. callId is not null.
if(list==null)
return false;
for (SipResponse sipResponse : list) {
if (sipResponse.getStatusCode() == statusCode)
return true;
}
return false;
}
}
在new SipResponseCollection()之后, volatile 映射是否可以为空?我在不同的机器上运行,其中一台机器提示 null 异常。但是删除关键字: volatile 后,一切正常。为什么?
ps:没有任何公共(public)方法可以将 map 设置为空。
最佳答案
不,它不能为 null
— 除非您设法从构造函数中泄漏 this
。你似乎没有这样做。
如果您向我们展示了更多代码,例如触发 NullPointerException
的代码,则可能会更具体地说明您的问题。
请注意,您的字段不是final
,因此该字段仍有可能在程序中的任何位置被清零。
关于java - java中的初始值之后, volatile 映射可以为空吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16832587/