Java手写锁Lock(公平、非公平)

public class MyLock2 implements Lock {

  private AtomicInteger state = new AtomicInteger(0);

  private Thread currentThread = null;

  Node head = null;
  Node tail = null;

  Sync sync;

  private class Node {
    Thread t;
    Node prev;
    Node next;

    Node() {
    }

    Node(Thread t) {
      this.t = t;
    }
  }

  public MyLock2() {
    sync = new NonFairLock();
  }

  public MyLock2(boolean fair) {
    if (fair) sync = new FairLock();
    else sync = new NonFairLock();
  }


  class Sync {

    public boolean tryLock() {
      if (state.compareAndSet(0, 1)) {
        currentThread = Thread.currentThread();
        return true;
      } else if (currentThread == Thread.currentThread()) {
        state.addAndGet(1);
        return true;
      }
      return false;
    }

    public void lock() {
      if (!tryLock()) {
        Node node = new Node(Thread.currentThread());
        addWaiters(node);

        for (; ; ) {
          if (node == head && tryLock()) {
            removeHead();
            return;
          } else {
            LockSupport.park();
          }
        }
      }
    }

    public void unlock() {
      if (currentThread != Thread.currentThread()) {
        throw new RuntimeException("非法调用");
      }
      if (state.decrementAndGet() == 0 && head != null) {
        LockSupport.unpark(head.t);
      }
    }
  }


  class FairLock extends Sync {

    public void lock() {
      if (state.get() != 0) {
        Node node = new Node(Thread.currentThread());
        addWaiters(node);

        for (; ; ) {
          if (node == head && tryLock()) {
            removeHead();
            return;
          } else {
            LockSupport.park();
          }
        }
      } else {
        tryLock();
      }

    }
  }


  class NonFairLock extends Sync {

  }


  @Override
  public void lock() {
    sync.lock();
  }

  @Override
  public void lockInterruptibly() throws InterruptedException {

  }

  @Override
  public boolean tryLock() {
    return false;
  }

  @Override
  public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
    return false;
  }

  @Override
  public void unlock() {
    sync.unlock();
  }

  @Override
  public Condition newCondition() {
    return null;
  }

  private void addWaiters(Node node) {
    if (tail == null) {
      head = node;
      tail = node;
    } else {
      tail.next = node;
      node.prev = tail;
      tail = tail.next;
    }
  }

  private void removeHead() {
    head = head.next;
    if (head == null) tail = null;
  }

  public static void main(String[] args) {
    MyLock2 lock = new MyLock2(true);

    Thread thread1 = new Thread(() -> {
      lock.lock();
      System.out.println("thread1");
      try {
        Thread.sleep(3000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      lock.unlock();
    });
    Thread thread2 = new Thread(() -> {
      lock.lock();
      System.out.println("thread2");
      try {
        Thread.sleep(3000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      lock.unlock();
    });

    thread1.start();
    thread2.start();
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值