当前位置:网站首页>多线程之享元模式和final原理
多线程之享元模式和final原理
2022-08-10 00:44:00 【七国的天下,我要九十九】
1 享元模式
1 定义
享元模式,Flyweight pattern, 当需要重用数量有限的同一类对象时使用.
2 体现
1 包装类
Jdk中Boolean, Byte, Short, Integer, Long, Character等包装类提供了valueOf方法.以Long的valueOf为例, 会缓存-128到127之间的Long对象,在此区间会重用对象,大于这个范围才会新建Long对象.
public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) {
// will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}
Byte,Short, Long缓存的范围都是-128到127
Character缓存的范围是0-127
Integer的默认范围是-128到127, 最小值不能变,最大值可以通过调整虚拟机参数
-Djava.lang.Integer.IntegerCache.high
来改变Boolean缓存了TRUE和FALSE
2 BigDecimal类
private static BigDecimal add(BigInteger fst, int scale1, BigInteger snd, int scale2) {
int rscale = scale1;
long sdiff = (long)rscale - scale2;
if (sdiff != 0) {
if (sdiff < 0) {
int raise = checkScale(fst,-sdiff);
rscale = scale2;
fst = bigMultiplyPowerTen(fst,raise);
} else {
int raise = checkScale(snd,sdiff);
snd = bigMultiplyPowerTen(snd,raise);
}
}
BigInteger sum = fst.add(snd);
return (fst.signum == snd.signum) ?
new BigDecimal(sum, INFLATED, rscale, 0) :
valueOf(sum, rscale, 0);
}
单个方法是采用保护性拷贝类方式, 是原子的,线程安全的, 但是不能保证多个方法的组合是原子的,线程安全的,所以在使用中,需要其他方法保证,如使用AtomicReference.
2 final原理
1 设置final的原理
根据之前volatile原理,对比final的实现.
public class TestFinal {
final int a = 20;
}
对应字节码文件:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: bipush 20
7: putfield #2 // Field a:I
<-- 添加了写屏障
10: return
发现 final 变量的赋值也会通过 putfield 指令来完成,同样在这条指令之后也会加入写屏障,保证在其它线程读到 它的值时不会出现为 0 的情况
2 获取final的原理
public class TestFinal {
final int A = 20;
final int B = Short.MAX_VALUE + 1;
static final int a = 20;
static final int b = Short.MAX_VALUE + 1;
}
根据编译的字节码文件可知, 当数据较小就在栈内存中获取,数据量超过最大,就在类常量池中.
3 无状态
在 web 中,设计 Servlet 时为了保证其线程安全,都会有这样的建议,不要为 Servlet 设置成员变量,这种没有任何成员变量的类是线程安全的
因为成员变量保存的数据也可以称为状态信息,因此没有成员变量就称之为【无状态】
边栏推荐
- 破产企业的职工退休怎么办?
- Interlay集成至Moonbeam,为网络带来interBTC和INTR
- 西安生物素-四聚乙二醇-酰胺-4苯酚 浅黄色半固态
- [obs] obsqsv11 hard coding and comparison with metartc codec
- Sikuli 基于图形识别的自动化测试技术
- Quick responsiveness intelligent/smart responsiveness of polyethylene glycol type nano/reduction response hydrogels research and preparation
- 跳房子游戏
- unity 报错 Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe‘ code“ in Pla
- 开发IM即时通讯容易吗?需要什么技术
- 由生物素参与的D-Biotinol,CAS号:53906-36-8具体特性说明
猜你喜欢
Win7怎么把控制面板添加到右键菜单
将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析
y92.第六章 微服务、服务网格及Envoy实战 -- Envoy基础(三)
Unity顶点动画
嵌入式Qt-实现两个窗口的切换
[LeetCode] Find the sum of the numbers from the root node to the leaf node
C language structure, function and pointer exercise (simple address book)
万字总结:分布式系统的38个知识点
C language pointer practice questions
Unity image使用长图后 图片很糊
随机推荐
UI遍历的初步尝试
eyb:Redis学习(4)
03|Process Control
嵌入式Qt-实现两个窗口的切换
Docker interview question 2--get the number of database connections and docker-compose
Interlay集成至Moonbeam,为网络带来interBTC和INTR
头脑风暴:单词拆分
人际关系不仅要“存”,更要“激活”!
@PostConsturct注解作用及特点
-red and black-
芯片资讯|半导体收入增长预计将放缓至 7%,蓝牙芯片需求依然稳步增长
mstsc/Mstsc (Microsoft terminal services client)远程桌面连接
Entity FrameWork Core教程,从基础应用到原理实战
Solidity最强对手:MOVE语言及新公链崛起
微信小程序tab切换时保存checkbox状态
基于设计稿识别的可视化低代码系统实践
R语言使用cox函数构建生存分析回归模型、使用subgroupAnalysis进行亚组分析并可视化森林图
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
什么是持续测试?
XSS详解及复现gallerycms字符长度限制短域名绕过