简体   繁体   English

当 LinkedList 大小为 -1 时

[英]When LinkedList size is -1

I'm facing this exception, in a java 1.7 project:我在 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

but i can't figure out in which situation the size of a LinkedList could be -1, isn't suppose to be a positive number?但我不知道在哪种情况下 LinkedList 的大小可能是 -1,不应该是正数吗? Any suggestion?有什么建议吗?

Custom Exception Handler code (row 74 is the for)自定义异常处理程序代码(第 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();) {
               ...
            }
        }
    }

I am able to reproduce the size of a LinkedList being -1 if I run this code couple of times:如果我多次运行此代码,我能够重现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();
        }
    }

As LinkedList is not thread-safe, trying to remove some elements from the LinkedList instance simultaneously from different threads might lead to the size being -1.由于LinkedList不是线程安全的,因此尝试从不同线程同时从LinkedList实例中删除某些元素可能会导致大小为 -1。

The only explanation that comes to mind is if the linked list is being passed over the wire and the sender explicitly sets its size as -1 in the payload.想到的唯一解释是,如果链表通过网络传递并且发送方在有效负载中明确设置其大小为 -1。

Then the endpoint would try and possibly succeed in setting the size to -1 via reflection.然后端点将尝试并可能成功通过反射将大小设置为 -1。

But is is a shot in the dark.但这是在黑暗中拍摄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM