zoukankan      html  css  js  c++  java
  • ProphetRouter出现异常——The One 模拟器

    ProphetRouter会遇到如下问题:

    Exception in thread "main" java.lang.IllegalArgumentException: Comparison method
    violates its general contract!
            at java.util.TimSort.mergeHi(Unknown Source)
            at java.util.TimSort.mergeAt(Unknown Source)
            at java.util.TimSort.mergeForceCollapse(Unknown Source)
            at java.util.TimSort.sort(Unknown Source)
            at java.util.TimSort.sort(Unknown Source)
            at java.util.Arrays.sort(Unknown Source)
            at java.util.Collections.sort(Unknown Source)
            at routing.ProphetRouter.tryOtherMessages(ProphetRouter.java:241)
            at routing.ProphetRouter.update(ProphetRouter.java:201)
            at core.DTNHost.update(DTNHost.java:330)
            at core.World.updateHosts(World.java:217)
            at core.World.update(World.java:186)
            at gui.DTNSimGUI.runSim(DTNSimGUI.java:115)
            at ui.DTNSimUI.start(DTNSimUI.java:77)
            at core.DTNSim.main(DTNSim.java:92)

    是由于:ProphetRouter.java中的TupleComparator 引起的:

    private class TupleComparator implements Comparator
            <Tuple<Message, Connection>> {

            public int compare(Tuple<Message, Connection> tuple1,
                    Tuple<Message, Connection> tuple2) {
                // delivery probability of tuple1's message with tuple1's connection
                double p1 = ((ProphetRouter)tuple1.getValue().
                        getOtherNode(getHost()).getRouter()).getPredFor(
                        tuple1.getKey().getTo());
                // -"- tuple2...
                double p2 = ((ProphetRouter)tuple2.getValue().
                        getOtherNode(getHost()).getRouter()).getPredFor(
                        tuple2.getKey().getTo());

                // bigger probability should come first
                if (p2-p1 == 0) {
                    /* equal probabilities -> let queue mode decide */
                   return compareByQueueMode(tuple1.getKey(), tuple2.getKey());
                }
                else if (p2-p1 < 0) {
                    return -1;
                }
                else {
                    return 1;
                }
            }
        }

    因为:java版本问题,java1.7使用TimSort,而TimSort发现comparable违反Comparable contract会抛出

    IllegalArgumentException。

    与这个问题类似:http://stackoverflow.com/questions/6626437/why-does-my-compare-method-throw-exception-comparison-method-violates-its-gen

    From http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#source

    Area: API: Utilities

    Synopsis: Updated sort behavior for Arrays and Collections may throw anIllegalArgumentException

    Description: The sorting algorithm used by java.util.Arrays.sort and (indirectly) byjava.util.Collections.sort has been replaced. The new sort implementation may throw anIllegalArgumentException if it detects a Comparable that violates the Comparable contract. The previous implementation silently ignored such a situation. If the previous behavior is desired, you can use the new system property, java.util.Arrays.useLegacyMergeSort, to restore previous mergesort behavior.

    Nature of Incompatibility: behavioral

    RFE: 6804124

    因此,作如下修改:

    在程序主函数加入一下一行代码:

    System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");

  • 相关阅读:
    flask-login
    python3安装scrapy框架
    Redis--对象共享(整数型字符串)
    Redis--对象(type、encoding、ptr、lru、refcount)
    Redis--内存回收(引用计数法)
    Redis--跳跃表
    Redis--压缩列表(节约内存,连锁更新)
    Redis--整数集合(升降级)
    Redis--Rehash(h[0],h[1],rehashIdx, 渐进式)
    Redis--解决Hash表键冲突(单向链表next指针,表头)
  • 原文地址:https://www.cnblogs.com/growup/p/2538245.html
Copyright © 2011-2022 走看看