synchronized加锁String踩坑日记
本文参考 https://juejin.im/post/59fffddc5188253d6816f9c1 JVM内存分布
我们知道String.intern()方法会判断该字符串是否存在常量池中,如果存在直接获取,不存在将当前字符串放到常量池中
String 分情况存在于常量池跟JVM堆中,首先让我们来看一下例子一:
public class TestStringSycn { public static void main(String[] args) { String a = "a"+"b"; System.out.println(a); } }
我们使用javap -c TestStringSycn.class
来看编译器如何帮我们解析的:
$ javap -c TestStringSycn.class
Compiled from "TestStringSycn.java"
public class TestStringSycn {
public TestStringSycn();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String ab
2: astore_1
3: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
6: aload_1
7: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
10: return
}
我们可以看到,编辑器直接是帮我们直接拼接在一起,也就是说这里其实会在常量池里面存在 ab 这个常量 2. 例子二:
public class TestStringSycn {
public static void main(String[] args) {
String a = "key";
String b = "zzz";
String c = a+ b;
System.out.println(c);
}
}
$ javap -c TestStringSycn.class
Compiled from "TestStringSycn.java"
public TestStringSycn {
public TestStringSycn();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String key
2: astore_1
3: ldc #3 // String zzz
5: astore_2
6: new #4 // class java/lang/StringBuilder
9: dup
10: invokespecial #5 // Method java/lang/StringBuilder."<init>":()V
13: aload_1
14: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
17: aload_2
18: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
21: invokevirtual #7 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
24: astore_3
25: getstatic #8 // Field java/lang/System.out:Ljava/io/PrintStream;
28: aload_3
29: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
32: return
}
我们可以看到,这里采用的是StringBuilder直接帮我们拼接而成,最后调用toString()方法,所以每次都会在堆上面生成一个String对象。
回到主题
当我们使用synchronized
加锁String的时候,我们需要保证当前加锁的key是唯一的,通过例子1,2知道。如果要加锁String,最好是加锁String.intern()方法。eg:
public class TestStringSycn {
public static void main(String[] args) {
String type = "order";
String id = "1";
String key = type + id;
synchronized (key.intern()) {
//执行方法
}
}
}