java中synchronized关键字分析

今天我们来分析一下java中synchronized关键字。首先来看一段java代码:(本地编译环境为mac,jdk1.8的环境)

Demo.java

 package com.example.springcloud.provider.demo;

 public class Demo {
private boolean isOk = true; public void test(){
isOk=false;
}
}

编译周后的Demo.class二进制文件:

Demo.class

 cafe babe 0000 0034 0015 0a00 0400 1109
0003 0012 0700 1307 0014 0100 0469 734f
6b01 0001 5a01 0006 3c69 6e69 743e 0100
0328 2956 0100 0443 6f64 6501 000f 4c69
6e65 4e75 6d62 6572 5461 626c 6501 0012
4c6f 6361 6c56 6172 6961 626c 6554 6162
6c65 0100 0474 6869 7301 002c 4c63 6f6d
2f65 7861 6d70 6c65 2f73 7072 696e 6763
6c6f 7564 2f70 726f 7669 6465 722f 6465
6d6f 2f44 656d 6f3b 0100 0474 6573 7401
000a 536f 7572 6365 4669 6c65 0100 0944
656d 6f2e 6a61 7661 0c00 0700 080c 0005
0006 0100 2a63 6f6d 2f65 7861 6d70 6c65
2f73 7072 696e 6763 6c6f 7564 2f70 726f
7669 6465 722f 6465 6d6f 2f44 656d 6f01
0010 6a61 7661 2f6c 616e 672f 4f62 6a65
6374 0021 0003 0004 0000 0001 0002 0005
0006 0000 0002 0001 0007 0008 0001 0009
0000 0038 0002 0001 0000 000a 2ab7 0001
2a04 b500 02b1 0000 0002 000a 0000 000a
0002 0000 0003 0004 0004 000b 0000 000c
0001 0000 000a 000c 000d 0000 0001 000e
0008 0001 0009 0000 0034 0002 0001 0000
0006 2a03 b500 02b1 0000 0002 000a 0000
000a 0002 0000 0007 0005 0008 000b 0000
000c 0001 0000 0006 000c 000d 0000 0001
000f 0000 0002 0010

使用javap -v Demo.class  > Demo.txt 文件得到反编译的汇编语言:

Demo.txt

 Classfile /Users/shiwen/IdeaProjects/springcloud/example-springcloud-provider/target/classes/com/example/springcloud/provider/demo/Demo.class
Last modified 2017-10-11; size 424 bytes
MD5 checksum 1f57b26a93d1cc466ff58bcec5ff37fb
Compiled from "Demo.java"
public class com.example.springcloud.provider.demo.Demo
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #4.#17 // java/lang/Object."<init>":()V
#2 = Fieldref #3.#18 // com/example/springcloud/provider/demo/Demo.isOk:Z
#3 = Class #19 // com/example/springcloud/provider/demo/Demo
#4 = Class #20 // java/lang/Object
#5 = Utf8 isOk
#6 = Utf8 Z
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lcom/example/springcloud/provider/demo/Demo;
#14 = Utf8 test
#15 = Utf8 SourceFile
#16 = Utf8 Demo.java
#17 = NameAndType #7:#8 // "<init>":()V
#18 = NameAndType #5:#6 // isOk:Z
#19 = Utf8 com/example/springcloud/provider/demo/Demo
#20 = Utf8 java/lang/Object
{
public com.example.springcloud.provider.demo.Demo();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_1
6: putfield #2 // Field isOk:Z
9: return
LineNumberTable:
line 3: 0
line 4: 4
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this Lcom/example/springcloud/provider/demo/Demo; public void test();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: iconst_0
2: putfield #2 // Field isOk:Z
5: return
LineNumberTable:
line 7: 0
line 8: 5
LocalVariableTable:
Start Length Slot Name Signature
0 6 0 this Lcom/example/springcloud/provider/demo/Demo;
}
SourceFile: "Demo.java"

修改Demo.java文件在方法中新增synchronized如下:

package com.example.springcloud.provider.demo;

public class Demo {
private boolean isOk = true; private synchronized void test(){
    isOk=false;
} }

反编译为汇编后:(不同的地方用黄色标识出来了)

 Classfile /Users/shiwen/IdeaProjects/springcloud/example-springcloud-provider/target/classes/com/example/springcloud/provider/demo/Demo.class
Last modified 2017-10-11; size 424 bytes
MD5 checksum 000d5f4eb139b3d12c483f7087c0c970
Compiled from "Demo.java"
public class com.example.springcloud.provider.demo.Demo
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #4.#17 // java/lang/Object."<init>":()V
#2 = Fieldref #3.#18 // com/example/springcloud/provider/demo/Demo.isOk:Z
#3 = Class #19 // com/example/springcloud/provider/demo/Demo
#4 = Class #20 // java/lang/Object
#5 = Utf8 isOk
#6 = Utf8 Z
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lcom/example/springcloud/provider/demo/Demo;
#14 = Utf8 test
#15 = Utf8 SourceFile
#16 = Utf8 Demo.java
#17 = NameAndType #7:#8 // "<init>":()V
#18 = NameAndType #5:#6 // isOk:Z
#19 = Utf8 com/example/springcloud/provider/demo/Demo
#20 = Utf8 java/lang/Object
{
public com.example.springcloud.provider.demo.Demo();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_1
6: putfield #2 // Field isOk:Z
9: return
LineNumberTable:
line 3: 0
line 4: 4
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this Lcom/example/springcloud/provider/demo/Demo; public synchronized void test();
descriptor: ()V
flags: ACC_PUBLIC, ACC_SYNCHRONIZED
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: iconst_0
2: putfield #2 // Field isOk:Z
5: return
LineNumberTable:
line 7: 0
line 8: 5
LocalVariableTable:
Start Length Slot Name Signature
0 6 0 this Lcom/example/springcloud/provider/demo/Demo;
}
SourceFile: "Demo.java"

改为synchronized代码块:

 package com.example.springcloud.provider.demo;

 public class Demo {
private boolean isOk = true; public void test() {
synchronized (this) {
isOk = false;
}
}
}

反编译汇编为:

 Classfile /Users/shiwen/IdeaProjects/springcloud/example-springcloud-provider/target/classes/com/example/springcloud/provider/demo/Demo.class
Last modified 2017-10-11; size 536 bytes
MD5 checksum 64305bd51f24f439444d9240efd483fd
Compiled from "Demo.java"
public class com.example.springcloud.provider.demo.Demo
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #4.#21 // java/lang/Object."<init>":()V
#2 = Fieldref #3.#22 // com/example/springcloud/provider/demo/Demo.isOk:Z
#3 = Class #23 // com/example/springcloud/provider/demo/Demo
#4 = Class #24 // java/lang/Object
#5 = Utf8 isOk
#6 = Utf8 Z
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lcom/example/springcloud/provider/demo/Demo;
#14 = Utf8 test
#15 = Utf8 StackMapTable
#16 = Class #23 // com/example/springcloud/provider/demo/Demo
#17 = Class #24 // java/lang/Object
#18 = Class #25 // java/lang/Throwable
#19 = Utf8 SourceFile
#20 = Utf8 Demo.java
#21 = NameAndType #7:#8 // "<init>":()V
#22 = NameAndType #5:#6 // isOk:Z
#23 = Utf8 com/example/springcloud/provider/demo/Demo
#24 = Utf8 java/lang/Object
#25 = Utf8 java/lang/Throwable
{
public com.example.springcloud.provider.demo.Demo();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_1
6: putfield #2 // Field isOk:Z
9: return
LineNumberTable:
line 3: 0
line 4: 4
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this Lcom/example/springcloud/provider/demo/Demo; public void test();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=3, args_size=1
0: aload_0
1: dup
2: astore_1
3: monitorenter
4: aload_0
5: iconst_0
6: putfield #2 // Field isOk:Z
9: aload_1
10: monitorexit
11: goto 19
14: astore_2
15: aload_1
16: monitorexit
17: aload_2
18: athrow
19: return
Exception table:
from to target type
4 11 14 any
14 17 14 any
LineNumberTable:
line 7: 0
line 8: 4
line 9: 9
line 10: 19
LocalVariableTable:
Start Length Slot Name Signature
0 20 0 this Lcom/example/springcloud/provider/demo/Demo;
StackMapTable: number_of_entries = 2
frame_type = 255 /* full_frame */
offset_delta = 14
locals = [ class com/example/springcloud/provider/demo/Demo, class java/lang/Object ]
stack = [ class java/lang/Throwable ]
frame_type = 250 /* chop */
offset_delta = 4
}
SourceFile: "Demo.java"

和synchronized方法对比如下:

java中synchronized关键字分析

java中synchronized关键字分析

给isOk增加volatile关键字后

 Classfile /Users/shiwen/IdeaProjects/springcloud/example-springcloud-provider/target/classes/com/example/springcloud/provider/demo/Demo.class
Last modified 2017-10-11; size 536 bytes
MD5 checksum 8dc910b015c7d5af2feac29d19f519a1
Compiled from "Demo.java"
public class com.example.springcloud.provider.demo.Demo
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #4.#21 // java/lang/Object."<init>":()V
#2 = Fieldref #3.#22 // com/example/springcloud/provider/demo/Demo.isOk:Z
#3 = Class #23 // com/example/springcloud/provider/demo/Demo
#4 = Class #24 // java/lang/Object
#5 = Utf8 isOk
#6 = Utf8 Z
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lcom/example/springcloud/provider/demo/Demo;
#14 = Utf8 test
#15 = Utf8 StackMapTable
#16 = Class #23 // com/example/springcloud/provider/demo/Demo
#17 = Class #24 // java/lang/Object
#18 = Class #25 // java/lang/Throwable
#19 = Utf8 SourceFile
#20 = Utf8 Demo.java
#21 = NameAndType #7:#8 // "<init>":()V
#22 = NameAndType #5:#6 // isOk:Z
#23 = Utf8 com/example/springcloud/provider/demo/Demo
#24 = Utf8 java/lang/Object
#25 = Utf8 java/lang/Throwable
{
public volatile boolean isOk;
descriptor: Z
flags: ACC_PUBLIC, ACC_VOLATILE public com.example.springcloud.provider.demo.Demo();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_1
6: putfield #2 // Field isOk:Z
9: return
LineNumberTable:
line 3: 0
line 4: 4
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this Lcom/example/springcloud/provider/demo/Demo; public void test();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=3, args_size=1
0: aload_0
1: dup
2: astore_1
3: monitorenter
4: aload_0
5: iconst_0
6: putfield #2 // Field isOk:Z
9: aload_1
10: monitorexit
11: goto 19
14: astore_2
15: aload_1
16: monitorexit
17: aload_2
18: athrow
19: return
Exception table:
from to target type
4 11 14 any
14 17 14 any
LineNumberTable:
line 7: 0
line 8: 4
line 9: 9
line 10: 19
LocalVariableTable:
Start Length Slot Name Signature
0 20 0 this Lcom/example/springcloud/provider/demo/Demo;
StackMapTable: number_of_entries = 2
frame_type = 255 /* full_frame */
offset_delta = 14
locals = [ class com/example/springcloud/provider/demo/Demo, class java/lang/Object ]
stack = [ class java/lang/Throwable ]
frame_type = 250 /* chop */
offset_delta = 4
}
SourceFile: "Demo.java"
上一篇:Java并发-Synchronized关键字


下一篇:日期转换时Safari中返回Invalid Date