synchronized 的性质
- 可重入性;
- 不可中断性
可重入性
- 同一线程的外层方法获得锁后,内层方法可以直接再次获取该锁;
- 避免死锁,提升封装性;
- 关键字:同一线程,同一把锁
可重入性示例(一) 同一个方法是可重入的
package com.example.concurrency.example.sync;
public class SynchronizedReentrant01 {
int a = 0;
public static void main(String[] args) {
SynchronizedReentrant01 instance = new SynchronizedReentrant01();
instance.method1();
}
private synchronized void method1() {
System.out.println("method1(), a = " + a);
if (a == 0) {
a ++;
method1();
}
}
}
输出:
method1(), a = 0
method1(), a = 1
可重入性示例(二) 可重入不要求是同一个方法
public class SynchronizedReentrant02 {
public static void main(String[] args) {
SynchronizedReentrant02 instance = new SynchronizedReentrant02();
instance.method1();
}
private synchronized void method1() {
System.out.println("method1()");
method2();
}
private synchronized void method2() {
System.out.println("method2()");
}
}
输出:
method1()
method2()
可重入性示例(三) 可重入不要求是同一类的方法
public class SynchronizedReentrant03 {
public synchronized void doSomething() {
System.out.println("Super doSomething()");
}
}
class Test extends SynchronizedReentrant03 {
@Override
public synchronized void doSomething() {
System.out.println("Sub doSomething()");
super.doSomething();
}
public static void main(String[] args) {
Test instance = new Test();
instance.doSomething();
}
}
输出:
Sub doSomething()
Super doSomething()
不可中断性
- 一旦这个锁已经被别人获得了,如果我还想获得,我只能选择等待或者阻塞,直到别的线程释放这个锁;如果别的线程永远不释放锁,那么我只能永远等待;
- 相比之下,Lock类提供的锁,拥有中断能力;第一,如果我觉得等待的时间太长了,有权中断现在已经获取到锁的线程的执行;第二,如果我觉得等待的时间太长了不想再等待,也可以退出;
synchronized 的缺点
- 效率低
- 锁的释放情况少,只在程序正常执行完成和抛出异常时释放锁;
- 试图获得锁是不能设置超时;
- 不能中断一个正在试图获得锁的线程;
- 无法知道是否成功获取到锁;