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

转载于:https://www.cnblogs.com/growup/archive/2012/06/06/2538245.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值