线程在BigDecimal.compareTo中挂起

时间:2014-03-03 23:18:03

标签: java

我们已经多次看过这个问题,有人可以解释一下这种情况是如何实现的,因为BigDecimal是不可变的吗?

 java.lang.Thread.State: RUNNABLE
2014/03/03 17:10:17.517 | INFO   | jvm 1    |   at java.math.BigInteger.<init>(Unknown Source)
2014/03/03 17:10:17.517 | INFO   | jvm 1    |   at java.math.BigDecimal.bigTenToThe(Unknown Source)
2014/03/03 17:10:17.517 | INFO   | jvm 1    |   at java.math.BigDecimal.bigDigitLength(Unknown Source)
2014/03/03 17:10:17.517 | INFO   | jvm 1    |   at java.math.BigDecimal.precision(Unknown Source)
2014/03/03 17:10:17.517 | INFO   | jvm 1    |   at java.math.BigDecimal.compareMagnitude(Unknown Source)
2014/03/03 17:10:17.517 | INFO   | jvm 1    |   at java.math.BigDecimal.compareTo(Unknown Source)

这怎么可能发生?

1 个答案:

答案 0 :(得分:1)

BigDecimal.bigTenToThe方法正在调用BigInteger构造函数 这就是它挂起的地方。请参阅源代码中的以下注释 这在很大程度上取决于你所称的论点。
另外,请参阅return语句。不确定你的联系 看到可变性。我在这里看不到任何东西。

/**
 * Return 10 to the power n, as a {@code BigInteger}.
 *
 * @param  n the power of ten to be returned (>=0)
 * @return a {@code BigInteger} with the value (10<sup>n</sup>)
 */
private static BigInteger bigTenToThe(int n) {
    if (n < 0)
        return BigInteger.ZERO;

    if (n < BIG_TEN_POWERS_TABLE_MAX) {
        BigInteger[] pows = BIG_TEN_POWERS_TABLE;
        if (n < pows.length)
            return pows[n];
        else
            return expandBigIntegerTenPowers(n);
    }
    // BigInteger.pow is slow, so make 10**n by constructing a
    // BigInteger from a character string (still not very fast)
    char tenpow[] = new char[n + 1];
    tenpow[0] = '1';
    for (int i = 1; i <= n; i++)
        tenpow[i] = '0';                
    return new BigInteger(tenpow);
}
相关问题