[英]When LinkedList size is -1
我在 java 1.7 項目中遇到了這個異常:
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: -1
at java.util.LinkedList.checkPositionIndex(LinkedList.java:558) ~[na:1.7.0_251]
at java.util.LinkedList.listIterator(LinkedList.java:865) ~[na:1.7.0_251]
at java.util.AbstractList.listIterator(AbstractList.java:299) ~[na:1.7.0_251]
at java.util.AbstractSequentialList.iterator(AbstractSequentialList.java:239) ~[na:1.7.0_251]
at com.project.exceptions.handlers.JsfExceptionHandler.handle(JsfExceptionHandler.java:74) ~[project-0.0.1-SNAPSHOT.jar:na]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119) ~[jsf-impl-2.1.13-redhat-1.jar!/:2.1.13-redhat-1]
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) ~[jsf-impl-2.1.13-redhat-1.jar!/:2.1.13-redhat-1]
at org.springframework.faces.webflow.FlowLifecycle.render(FlowLifecycle.java:80) ~[spring-faces-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.faces.webflow.JsfView.render(JsfView.java:89) ~[spring-faces-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.webflow.engine.ViewState.render(ViewState.java:296) ~[spring-webflow-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.webflow.engine.ViewState.resume(ViewState.java:207) ~[spring-webflow-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.webflow.engine.Flow.resume(Flow.java:545) [spring-webflow-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.webflow.engine.impl.FlowExecutionImpl.resume(FlowExecutionImpl.java:258) [spring-webflow-2.3.2.RELEASE.jar:2.3.2.RELEASE]
... 62 common frames omitted
但我不知道在哪種情況下 LinkedList 的大小可能是 -1,不應該是正數嗎? 有什么建議嗎?
自定義異常處理程序代碼(第 74 行是 for)
public class JsfExceptionHandler extends ExceptionHandlerWrapper {
...
@Override
public void handle() throws FacesException {
FacesContext fc = ContextUtils.getFacesContextInstance();
for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
...
}
}
}
如果我多次運行此代碼,我能夠重現LinkedList
的大小為 -1:
try {
LinkedList<Integer> integers = new LinkedList<>();
integers.add(1);
new Thread(() -> {
try {
Thread.sleep(1950);
integers.removeFirst();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
Thread.sleep(1950);
integers.removeFirst();
System.out.println(integers.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
由於LinkedList
不是線程安全的,因此嘗試從不同線程同時從LinkedList
實例中刪除某些元素可能會導致大小為 -1。
想到的唯一解釋是,如果鏈表通過網絡傳遞並且發送方在有效負載中明確設置其大小為 -1。
然后端點將嘗試並可能成功通過反射將大小設置為 -1。
但這是在黑暗中拍攝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.