今天查看log日志突然发现有个异常如下:
java.util.NoSuchElementException: null
at java.util.LinkedList.removeFirst
看了下我的代码
java
LinkedList<String> orderIdList = new LinkedList<>();
if(!orderIdList.contains(id)){
if(orderIdList.size()>100)orderIdList.removeFirst();
orderIdList.add());
firstFalg=1;
}
突然想到LinkedList是线程不安全的,看了下removeFirst 得底层代码
java
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
如果多线程访问,LinkedList内部结构被修改,则可能会抛出NoSuchElementException异常。
所以修改成
java
LinkedList<String> orderIdList = new LinkedList<>();
if(!orderIdList.contains(id)){
if(orderIdList.size()>100)orderIdList.remove(0);
orderIdList.add());
firstFalg=1;
}
问题解决。