当前位置:网站首页>多线程之享元模式和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 设置成员变量,这种没有任何成员变量的类是线程安全的
因为成员变量保存的数据也可以称为状态信息,因此没有成员变量就称之为【无状态】
边栏推荐
猜你喜欢

Win11怎么关闭系统保护功能?系统保护还原功能怎么关闭?

【CAS:41994-02-9 |Biotinyl tyramide】Biotinyl tyramide price

PEG derivative Biotin-PEG1-OH (cas: 95611-10-2, 2-biotinaminoethanol) advantage description
![[LeetCode] Find the sum of the numbers from the root node to the leaf node](/img/1c/6c627e17a60f424601226504f4ff36.png)
[LeetCode] Find the sum of the numbers from the root node to the leaf node

Docker 面试题2则--取数据库连接数和docker-compose

CAS:183896-00-6 (Biotin-PEG3-C3-NH2) PEG derivative

y92.第六章 微服务、服务网格及Envoy实战 -- Envoy基础(三)

Quick responsiveness intelligent/smart responsiveness of polyethylene glycol type nano/reduction response hydrogels research and preparation

【CAS:41994-02-9 |Biotinyl tyramide】生物素基酪氨酰胺价格

Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection
随机推荐
Character Statistics Histogram
Shader Graph学习各种特效案例
Minimum number of steps to get out of the maze 2
信息化和数字化的核心差异
开发IM即时通讯容易吗?需要什么技术
高校就业管理系统设计与实现
22.括号生成
CMake 编译运行dpdk项目程序
Biotin-Cy2 Conjugate,生物素-Cy2 偶联物_Cy2 生物素偶联物
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
微信小程序tab切换时保存checkbox状态
手把手教你编写性能测试用例
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
C language pointer practice questions
鲜花线上销售管理系统的设计与实现
CAS:851113-28-5 (Biotin-ahx-ahx-tyramine)
-红与黑-
03|Process Control
egg.js中Class constructor BaseContextClass cannot be invoked without ‘new‘解决方法
【CAS:41994-02-9 |Biotinyl tyramide】生物素基酪氨酰胺价格