启动器的学习

一、核心思想

  

二、启动流程

  

三、依赖关系的代码

  

 

四、重入锁的使用

  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();

}

 

posted on 2019-10-13 23:35  zhang11111wei  阅读(124)  评论(0编辑  收藏  举报

导航