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");

  • 相关阅读:
    py2与py3的字符编码的区别
    深浅拷贝大法
    列表、字典、元组、集合的内置方法
    数据类型的内置方法
    与while和for的爱恨情仇
    三种格式化输出以及运算符和if
    在outlook中发邮件判断邮件发送成功的方法
    修練營ASP.NET]淺談多層式架構 (Multi Tiers)
    软考编译原理小结
    0型文法、1型文法、2型文法、3型文法
  • 原文地址:https://www.cnblogs.com/growup/p/2538245.html
Copyright © 2011-2022 走看看