0
这样加锁会导致什么问题?
source link: https://www.v2ex.com/t/816914
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
String lock = userId;
synchronized(lock.intern()){
}
userId 是唯一的。
这样加锁常量池的数据会很庞大,会被 GC 及时回收吗
synchronized(lock.intern()){
}
userId 是唯一的。
这样加锁常量池的数据会很庞大,会被 GC 及时回收吗
9 条回复 • 2021-11-21 20:21:26 +08:00
fkdog 17 小时 46 分钟前
Integer a = 999992;
System.out.println(a.toString()==a.toString());
你 run 一下就会发现这个结果在 jdk1.8 以上就是 false 。
不是所有字符串都会建在常量池里
System.out.println(a.toString()==a.toString());
你 run 一下就会发现这个结果在 jdk1.8 以上就是 false 。
不是所有字符串都会建在常量池里
SachinBeyond 17 小时 32 分钟前
Constant expressions of type String are always "interned" so as to share unique
instances, using the method String.intern.
instances, using the method String.intern.
qaqLjj 11 小时 5 分钟前
这样写代码不优雅,字符串常量池本来是用来存储编译时可以确定的字符串常量的,你这样写相当于运行时往常量池里放东西,建议使用 new Lock(userId); 代替
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK