可重入锁
可重入锁
可重入锁就是一个线程已经获得了一把锁,可以重复获取而不会导致死锁
synchronized 和Lock都是可重入锁
synchronized 可重入锁示例
package com.example.juc;
public class TestReentrantLock {
public static void main(String[] args) {
Phone1 phone1 = new Phone1();
new Thread(() -> {
phone1.sendSms();
}, "A").start();
new Thread(() -> {
phone1.sendSms();
}, "B").start();
}
}
class Phone1 {
public synchronized void sendSms() {
System.out.println(Thread.currentThread().getName() + "sms");
call();
}
public synchronized void call() {
System.out.println(Thread.currentThread().getName() + "call");
}
}
Lock 可重入锁示例
package com.example.juc;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class TestReentrantLock2 {
public static void main(String[] args) {
Phone2 phone1 = new Phone2();
new Thread(() -> {
phone1.sendSms();
}, "A").start();
new Thread(() -> {
phone1.sendSms();
}, "B").start();
}
}
class Phone2 {
private Lock lock = new ReentrantLock();
public void sendSms() {
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "sms");
call();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void call() {
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "call");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
synchronized vs. Lock
- synchronized是一个java关键字,Lock是一个类
- synchronized可以修饰方法和代码块,Lock只能修饰代码块
- 使用方式也不一样,synchronized自动加锁解锁,Lock需要手动加锁解锁
- Lock加锁和解锁是配对的,否则会出现死锁的情况
死锁情况
package com.example.juc;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class TestReentrantLock2 {
public static void main(String[] args) {
Phone2 phone1 = new Phone2();
new Thread(() -> {
phone1.sendSms();
}, "A").start();
new Thread(() -> {
phone1.sendSms();
}, "B").start();
}
}
class Phone2 {
private Lock lock = new ReentrantLock();
public void sendSms() {
lock.lock();
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "sms");
call();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void call() {
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "call");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
Asms
Acall
...
这样也能化解死锁
package com.example.juc;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class TestReentrantLock2 {
public static void main(String[] args) {
Phone2 phone1 = new Phone2();
new Thread(() -> {
phone1.sendSms();
}, "A").start();
new Thread(() -> {
phone1.sendSms();
}, "B").start();
}
}
class Phone2 {
private Lock lock = new ReentrantLock();
public void sendSms() {
lock.lock();
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "sms");
call();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void call() {
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "call");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
lock.unlock();
}
}
}
Asms
Acall
Bsms
Bcall
但是如果线程B改成phone1.call(),代码会报错
Asms
Acall
Bcall
Exception in thread "B" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:151)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1263)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:457)
at com.example.juc.Phone2.call(TestReentrantLock2.java:44)
at com.example.juc.TestReentrantLock2.lambda$main$1(TestReentrantLock2.java:13)
at java.lang.Thread.run(Thread.java:748)