从被锁对象的性质上来说,synchronized加的锁分两种:实例对象锁和class对象锁。
实例对象锁
class Foo {
synchronized public void methodA() throws InterruptedException {
System.out.println("=====methodA Start====");
Thread.sleep(5000);
System.out.println("=====methodA END====");
}
public void methodB()throws InterruptedException {
synchronized (this){
System.out.println("=====methodB Start====");
Thread.sleep(5000);
System.out.println("=====methodB End====");
}
}
}
这两种方式加的是同一把锁,都是给当前class的实例加锁。
public class SynchronizeTest {
public static void main(String[] args) throws InterruptedException {
Foo f = new Foo();
Thread a = new Thread(new Runnable() {
@Override
public void run() {
try {
f.methodA();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread b = new Thread(new Runnable() {
@Override
public void run() {
try {
f.methodB();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread g = new Thread(new Runnable() {
@Override
public void run() {
synchronized (f){
System.out.println("=====main g Start====");
try {
Thread.sleep(5000);
f.methodA();
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
System.out.println("=====main g End====");
}
}
});
a.start();
g.start();
b.start();
}
}
如果有两个线程a和b分别调用Foo的这两个方法,那么他们会排队同步执行。
另外上面代码的线程g对f实例也加上了锁,所以最终a,g,b线程会排队依次执行。
class对象锁
synchronized public static void methodC()throws InterruptedException {
System.out.println("=====methodC Start====");
Thread.sleep(5000);
System.out.println("=====methodC End====");
}
public void methodD()throws InterruptedException {
synchronized (Foo.class){
System.out.println("=====methodD Start====");
Thread.sleep(5000);
System.out.println("=====methodD End====");
}
}
在Foo里加上methodC,methodD方法,类似的在main函数中使用线程c和d去调用的话,c和d也会排队依次执行。也就是说对静态方法使用synchronized,锁住的对象其实是class类型对象。
在JVM中,对象的实例和class对象是分开存放的,实例的markword里会有指针(Klass point)指向class对象,以表明该实例的类型是对应的class。那么分别对这两个地方加锁其实是不会互斥的。也就是说methodA和methodC可以并发执行。
最后,在方法上使用synchronized,生成的字节码指令会在在方法的flag标志位加上ACC_SYNCHRONIZED。而在代码块中使用,则在对应的代码块加上monitorenter和monitorexit。JVM在执行有ACC_SYNCHRONIZED标志的方法的时候会先去获取monitor对象,本质上就是利用monitor去锁住对象。
使用javap -c -v Foo.class
查看生成的字节码信息。
methodA()
public synchronized void methodA() throws java.lang.InterruptedException;
descriptor: ()V
flags: (0x0021) ACC_PUBLIC, ACC_SYNCHRONIZED
Code:
stack=2, locals=1, args_size=1
0: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #13 // String =====methodA Start====
5: invokevirtual #15 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: ldc2_w #21 // long 5000l
11: invokestatic #23 // Method java/lang/Thread.sleep:(J)V
14: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
17: ldc #29 // String =====methodA END====
19: invokevirtual #15 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
22: return
LineNumberTable:
line 6: 0
line 7: 8
line 8: 14
line 9: 22
LocalVariableTable:
Start Length Slot Name Signature
0 23 0 this Lcom/example/demo/thread/Foo;
Exceptions:
throws java.lang.InterruptedException
methodD()
public void methodD() throws java.lang.InterruptedException;
descriptor: ()V
flags: (0x0001) ACC_PUBLIC
Code:
stack=2, locals=3, args_size=1
0: aload_0
1: dup
2: astore_1
3: monitorenter
4: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
7: ldc #39 // String =====methodD Start====
9: invokevirtual #15 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
12: ldc2_w #21 // long 5000l
15: invokestatic #23 // Method java/lang/Thread.sleep:(J)V
18: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
21: ldc #41 // String =====methodD End====
23: invokevirtual #15 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
26: aload_1
27: monitorexit
28: goto 36
31: astore_2
32: aload_1
33: monitorexit
34: aload_2
35: athrow
36: return
Exception table:
from to target type
4 28 31 any
31 34 31 any
LineNumberTable:
line 26: 0
line 27: 4
line 28: 12
line 29: 18
line 30: 26
line 32: 36
LocalVariableTable:
Start Length Slot Name Signature
0 37 0 this Lcom/example/demo/thread/Foo;
StackMapTable: number_of_entries = 2
frame_type = 255 /* full_frame */
offset_delta = 31
locals = [ class com/example/demo/thread/Foo, class java/lang/Object ]
stack = [ class java/lang/Throwable ]
frame_type = 250 /* chop */
offset_delta = 4
Exceptions:
throws java.lang.InterruptedException
对象的wait和notify的方法也是通过monitor来实现的,在wait执行后会立即释放该对象的锁,这时候对象的控制权会转移到其他线程。所以假如有一个methodE类似这样:
public void methodE()throws InterruptedException {
synchronized (this) {
System.out.println("=====methodE Start====");
Thread.sleep(3000);
this.wait(); //此处释放锁
System.out.println("=====methodE End====");
}
}
走到this.wait();
后会释放锁,这时候其他竞争中的线程(比如methodA)则会拿到这把锁开始执行他们自己的事。