String str="abc" 一定会进入到字符串常量池吗?
source link: https://www.v2ex.com/t/788338
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 str="abc" 一定会进入到字符串常量池吗?
如果一定会,为什么我对 str 加锁,锁并没有用。
Yimkong 12 小时 17 分钟前 via iPhone
String str=new String("abc"),锁这个就行
Cy1 12 小时 11 分钟前 1
public static void main(String[] args) {
String lock1 = "abc";
String lock2 = "abc";
new Thread(() -> {
synchronized (lock1) {
System.out.println("t1");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t1").start();
new Thread(() -> {
synchronized (lock2) {
System.out.println("t2");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t2").start();
}
其中一个线程会被阻塞,明显就是锁的是同一个对象,所以会进 字符串常量池
Recommend
-
46
面试Linux运维一定会问到Shell脚本的这24个问题-51CTO.COM 面试Linux运维一定会问到Shell脚本的这24个问题 作者:莎莎 2017-11-10 13:30:31 虽然现在Python在运维工作中已经使用很普...
-
50
为何说比特币注定会崩盘?为何说比特币注定会崩盘?2017-12-10 11:25科技领域爱好者比特币的价格今年以来持续暴涨,似乎上涨无止境,不过如果能深刻理解货币对国家的重要性...
-
83
String是Java基础的重要考点。可问的点多,而且很多点可以横向切到其他考点,或纵向深入JVM。 本文略过了String的基本内容,重点在于String#intern()。 String常量池String常量可能会在两种时...
-
54
新浪科技讯北京时间3月7日消息,据国外媒体报道,长时间以来,物理学家一直坚称人类不可能揭开黑洞之谜。任何物体一旦进入黑洞便一去不返,无法开展研究。少量物质和能量也许能够以“霍金辐射”的形式逃离黑洞的魔爪,但任何落入黑洞的物体都将从实体宇宙中
-
39
新浪科技讯8月10日消息,2018年8月10-12日,以“共享全球智慧,引领未来科技”为主题的世界科技创新论坛将在北京会议中心举办,包括KipThorne、ThomasJ。Sargent、MichaelLevitt、朱棣文在内的20余位诺贝
-
50
-
7
【面试题】Java String常量相等(==)问题 以下三个结果分别输出(true or false)?别小看它,很多程序员因为上面问题出过生产bug String s3 = "s";String s4 = "s";System.out.println(s3==s4);
-
10
老套的笔试题 在一些老套的笔试题中,会要你判断s1==s2为false还是true,s1.equals(s2)为false还是true。 String s1 = new String("xyz"); String s2 = "xyz"; System.out.println(s1 == s2); System.out.println(s...
-
8
字符...
-
0
GitHub 19k Star 的Java工程师成神之路,不来了解一下吗! 在之前的一篇文章中,介绍过Java中的Class常量池,在Java...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK