一、核心思想
二、启动流程
三、依赖关系的代码
四、重入锁的使用
public class MyLock implements Lock {
private boolean isLocked = false;
Thread lockBy = null;
int lockCount = 0;
@Override
public synchronized void lock() {
Thread currentThread = Thread.currentThread();
lockBy = Thread.currentThread();
while (isLocked && lockBy != lockBy){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
isLocked = true;
lockBy = currentThread;
lockCount++;
}
@Override
public synchronized void unlock() {
if (lockBy == Thread.currentThread()) {
lockCount--;
if (lockCount == 0) {
notify();
isLocked = false;
lockCount = 0;
}
}
}
MyLock lock = new MyLock();
public void a(){
lock.lock();
System.out.println("a");
b();
lock.unlock();
}
public void b(){
lock.lock();
System.out.println("b");
lock.unlock();
}