![](/img/trans.png)
[英]BigDecimal - MathContext.DECIMAL64 vs MathContext.DECIMAL128
[英]MathContext.DECIMAL32 vs MathContext.DECIMAL64, which one to use, and why?
我應該使用MathContext.DECIMAL32
還是MathContext.DECIMAL64
嗎? 我看過文檔 ,但是我真的不明白何時使用它們。
我正在使用BigDecimal表示要應用於一定金額的百分比。 像這樣:
...
final MathContext mc = MathContext.DECIMAL32;
BigDecimal amount = getAmount(args);
float percent = getPercent().floatValue();
BigDecimal percentAsBd = new BigDecimal(percent/100.f, mc).setScale(4, RoundingMode.HALF_UP);
BigDecimal threshold = amount.multiply(percentAsBd);
...
我正在使用Oracle Java 1.8,Ubuntu 14.04,Intel Core i7(64bit)
根據您系統的體系結構,如果不使用x64芯片組,則任何64位類型操作的指令集都將分配給兩個CPU。 使用您的Intel Core i7(x64),可以解決與此相關的所有問題。
更新時間:2016年1月9日
根據JVM規范,對任何64位值的分配都需要兩個32位分配。
public class IdGenerator {
private long id;
public IdGenerator() {
id = 0;
}
public int getNextId() {
++value;
}
}
基於該假設,上述對getNextId的調用不是原子的。 如果在多線程上下文中使用此類,則結果getNextId()可能不完全准確,例如,這些調用可能會產生以下ID 0、1、3、5、6、7、8、10。 在x86平台上使用32位類型將不會出現此行為。
更新5/9/2016
希望以下鏈接對我的回答有所幫助
http://preshing.com/20130618/atomic-vs-non-atomic-operations/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.