bigdecimal

如何合理地估算线程池大小?

六眼飞鱼酱① 提交于 2019-11-30 00:14:55
感谢网友【 蒋小强 】投稿。 如何合理地估算线程池大小? 这个问题虽然看起来很小,却并不那么容易回答。大家如果有更好的方法欢迎赐教,先来一个天真的估算方法:假设要求一个系统的TPS(Transaction Per Second或者Task Per Second)至少为20,然后假设每个Transaction由一个线程完成,继续假设平均每个线程处理一个Transaction的时间为4s。那么问题转化为: 如何设计线程池大小,使得可以在1s内处理完20个Transaction? 计算过程很简单,每个线程的处理能力为0.25TPS,那么要达到20TPS,显然需要20/0.25=80个线程。 很显然这个估算方法很天真,因为它没有考虑到CPU数目。一般服务器的CPU核数为16或者32,如果有80个线程,那么肯定会带来太多不必要的线程上下文切换开销。 再来第二种简单的但不知是否可行的方法(N为CPU总核数): 如果是CPU密集型应用,则线程池大小设置为N+1 如果是IO密集型应用,则线程池大小设置为2N+1 如果一台服务器上只部署这一个应用并且只有这一个线程池,那么这种估算或许合理,具体还需自行测试验证。 接下来在这个文档:服务器性能IO优化 中发现一个估算公式: 最佳线程数目 = ((线程等待时间+线程CPU时间)/线程CPU时间 )* CPU数目 比如平均每个线程CPU运行时间为0.5s

9.16日上课总结

痴心易碎 提交于 2019-11-29 23:44:47
动手动脑总结: 1.1 仔细阅读示例: EnumTest.java ,运行它,分析运行结果? numTest.java 1 public class EnumTest { 2 3 public static void main(String[] args) { 4 Size s=Size.SMALL; 5 Size t=Size.LARGE; 6 //s和t引用同一个对象? 7 System.out.println(s==t); // 8 //是原始数据类型吗? 9 System.out.println(s.getClass().isPrimitive()); 10 //从字符串中转换 11 Size u=Size.valueOf("SMALL"); 12 System.out.println(s==u); //true 13 //列出它的所有值 14 for(Size value:Size.values()){ 15 System.out.println(value); 16 } 17 } 18 19 } 20 enum Size{SMALL,MEDIUM,LARGE}; 运行结果为: false false true SMALL MEDIUM LARGE 1.2. 你能得到什么结论?你掌握了枚举类型的基本用法了吗? 结论:枚举类型调用的不是同一个对象,枚举类型可以调用

20190918Java课堂记录

↘锁芯ラ 提交于 2019-11-29 23:37:09
1. EnumTest.java public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Size t=Size.LARGE; //s和t引用同一个对象? System.out.println(s==t); // //是原始数据类型吗? System.out.println(s.getClass().isPrimitive()); //从字符串中转换 Size u=Size.valueOf("SMALL"); System.out.println(s==u); //true //列出它的所有值 for(Size value:Size.values()){ System.out.println(value); } } } enum Size{SMALL,MEDIUM,LARGE}; s 和 t 的值不同,s == t 的结果为 false Size 不是基本类型,返回 false 枚举类型值相等即为一个变量 values() 为返回所有值 总结:枚举enum是一种特殊的类(还是类),使用枚举可以很方便的定义常量 一个常用的场合就是 switch 语句中,使用枚举来进行判断 注:因为是常量,所以一般都是全大写 使用枚举的好处就是可以把变量的范围限定在几个以内,在使用

BigDecimal

喜你入骨 提交于 2019-11-29 20:51:27
divide除法用法 a.divide(b.2.RoundingMode,HALF_UP) 补充: // BigDecimal方式 double f = 1025.435; BigDecimal b = new BigDecimal(new Double(f).toString); double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 注意:这里一定不要直接使用new BigDecimal(double)的构造方法, 而要使用new BigDecimal(new Double(1025.435).toString())的方式,不然会出现精确问题 原文链 接:https://blog.csdn.net/qq_26295547/article/details/82493231 解决方案 在做做除法的时候指定保留的小数的位数: BigDecimal rate = new BigDecimal(1).divide(new BigDecimal(3), 6, BigDecimal.ROUND_HALF_UP);做除法的时候出现了无限不循环小数如:0.333333333333 边际利率下有效认购总额 边际差额= 边际倍数=边际理论的有效认购量/边际差额 substract减法用法 BigDecimal的初始化

JavaUtils:四目运算(加减乘除)

夙愿已清 提交于 2019-11-29 19:41:21
import org . apache . commons . lang3 . StringUtils ; import java . math . BigDecimal ; import java . math . RoundingMode ; public class ArithmeticUtil { private final static String ZERO = "0" ; /** * 功能描述: * <小数相加,参数为空当0处理> * * @param str1 1 * @param str2 2 * @return java.lang.String * @author zhoulipu * @date 2019/8/27 19:58 */ public static String additionStr ( String str1 , String str2 ) { try { BigDecimal bd1 = new BigDecimal ( StringUtils . isBlank ( str1 ) ? "0" : str1 ) ; BigDecimal bd2 = new BigDecimal ( StringUtils . isBlank ( str2 ) ? "0" : str2 ) ; return bd1 . add ( bd2 ) .

BigDecimal

筅森魡賤 提交于 2019-11-29 19:11:13
BigDecimal 概念 BigDecimal = 大的小数。用于表示精确的小数,常用于财务计算。 和 BigInteger 类似, BigDecimal 可以表示一个任意大小且精度完全准确的浮点数。 基础 BigDecimal bd = new BigDecimal"123.4567"); BigDecimal用scale()表示小数位数,例如: BigDecimal d1 = new BigDecimal("123.45"); BigDecimal d2 = new BigDecimal("123.4500"); BigDecimal d3 = new BigDecimal("1234500"); System.out.println(d1.scale()); // 2,两位小数 System.out.println(d2.scale()); // 4 System.out.println(d3.scale()); // 0 stripTrailingZeros()方法,去掉末尾的0 BigDecimal d1 = new BigDecimal("123.4500"); BigDecimal d2 = d1.stripTrailingZeros(); System.out.println(d1.scale()); // 4 System.out.println(d2

Format of BigDecimal number

岁酱吖の 提交于 2019-11-29 12:09:24
BigDecimal val = BigDecimal.valueOf(0.20); System.out.println(a); I want to store in val a value 0.20 and not 0.2 . What I can do ? I dont think I can use NumberFormat in this case, when I use NumberFormat I must know what the length of my decimal number is! I can have 0.20 or 0.5000, I don't know the exact length of my decimal number, so I cannot use : DecimalFormat df = new DecimalFormat("#0.00"); or DecimalFormat df = new DecimalFormat("#0.00000"); maybe I have just 2 numbers after point or 5 numbers or more, and this program doesn't work: BigDecimal a = BigDecimal.valueOf(0.20);//i give an

Is there a difference between BigDecimal(“0”) and BigDecimal.ZERO?

冷暖自知 提交于 2019-11-29 10:56:24
问题 Either for comparisons or initialization of a new variable, does it make a difference which one of these you use? I know that BigDecimal.ZERO is a 1.5 feature, so that's a concern, but assuming I'm using 1.5 does it matter? Thanks. 回答1: BigDecimal.ZERO is a predefined constant and therefore doesn't have to be evaluated from a string at runtime as BigDecimal("0") would be. It will be faster and won't require creation of a new object. If your code needs to run on pre-1.5, then you can use the

Generating random BigDecimal value from given range

帅比萌擦擦* 提交于 2019-11-29 10:54:49
I need to generate random BigDecimal value from given range. How to do it in Java? class BigDecRand { public static void main(String[] args) { String range = args[0]; BigDecimal max = new BigDecimal(range + ".0"); BigDecimal randFromDouble = new BigDecimal(Math.random()); BigDecimal actualRandomDec = randFromDouble.divide(max,BigDecimal.ROUND_DOWN); BigInteger actualRandom = actualRandomDec.toBigInteger(); } } I do this that way public static BigDecimal generateRandomBigDecimalFromRange(BigDecimal min, BigDecimal max) { BigDecimal randomBigDecimal = min.add(new BigDecimal(Math.random())

Hibernate - BigDecimal column mapping for Custom Dialect

微笑、不失礼 提交于 2019-11-29 09:57:28
I'm using Hibernate as our Object-Relational Mapping, with a custom dialect for an obscure database. The Entity I'm retrieving from this database has a column thus: @Column(name = "GROSS_WEIGHT", precision = 9, scale = 3) private BigDecimal grossWeight; The database has this column defined as numeric with a precision of 9 and a scale of 3. I can see the SQL that Hibernate generates to retrieve the data, and when I perform the same Query using the database query tool it returns '9.68' for the GROSS_WEIGHT column. However, in the Entity that is retrieved by Hibernate, the 'grossWeight' field