当前位置:   article > 正文

两种方式手写ReentrantLock_太阳why手写

太阳why手写

第一种

public class MyReentrantLock implements Lock {

    private Sync sync = new Sync();

    @Override
    public void lock() {
        sync.acquire(1);
    }
 
    @Override
    public void lockInterruptibly() throws InterruptedException {
        sync.acquireInterruptibly(1);
    }
 
    @Override
    public boolean tryLock() {
        return sync.tryAcquire(1);
    }
 
    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return sync.tryAcquireNanos(1,time);
    }
 
    @Override
    public void unlock() {
        sync.release(1);
    }
 
    @Override
    public Condition newCondition() {
        return sync.newCondition();
    }
 
    /**
     * AbstractQueuedSynchronizer,重写tryAcquire,tryRelease方法.就可以实现可重入独占锁。
     */
    public class Sync extends AbstractQueuedSynchronizer {

        /**
         * 尝试获取锁逻辑
         * @param arg
         * @return
         */
        @Override
        protected boolean tryAcquire(int arg) {
            // 0是上次的值,用来判断是都已经被修改,1要更新的值,compareAndSetState是AbstractQueuedSynchronizer类的方法,底层采用CAS自旋
            if(compareAndSetState(0,1)){
                setExclusiveOwnerThread(Thread.currentThread());
                return true;
            }
            if(getExclusiveOwnerThread() == Thread.currentThread()){
                setState(getState()+1);
                return true;
            }
            return false;
        }
 
        /**
         *尝试释放所
         * @param arg
         * @return
         */
        @Override
        protected boolean tryRelease(int arg) {
            if(getExclusiveOwnerThread()!= Thread.currentThread()){
                throw new RuntimeException();
            }
            int state = getState() - 1;
            // 减少可重入次数
            if(state > 0){
                setState(state);
                return false;
            }
            // 释放锁
            setState(state);
            setExclusiveOwnerThread(null);
            return true;
        }


    Condition newCondition() {
         return new ConditionObject();
    }
 
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

测试

public static void main(String[] args) {
    MyReentrantLock myReentrantLock = new MyReentrantLock();
    myReentrantLock.tryLock();
    System.out.println("业务代码");
    myReentrantLock.unlock();
    System.out.println("第一次结束");

    System.out.println("再来一次");

    myReentrantLock.tryLock();
    System.out.println("业务代码");
    myReentrantLock.unlock();

    System.out.println("第二次结束");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

第二种

package lock;

import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;

/**
 * @author lwh
 * @create 2023-02-26 11:08
 */

public class MyLock {

    private AtomicInteger atomicInteger = new AtomicInteger(0);
    private Thread thread;
    // 存放阻塞的线程
    ConcurrentLinkedDeque<Thread> concurrentLinkedDeque = new ConcurrentLinkedDeque<>();

    public void lock(){
        acquire();
    }

    public boolean acquire() {
        for (;;){                       // 自旋,死循环是为了唤醒后可以再获取锁,
            if (compareAndSet(0,1)) {   // 获取到锁
                thread = Thread.currentThread();
                return true;
            }
            // 没获取到锁,进入队列,阻塞
            Thread thread = Thread.currentThread();
            concurrentLinkedDeque.add(Thread.currentThread());
            LockSupport.park(thread);
            return false;
        }
    }

    public boolean compareAndSet(int expect, int update){
        return atomicInteger.compareAndSet(expect,update);
    }


    public boolean unLock(){
       if (thread == null) {
           return false;
       }
       if (thread == Thread.currentThread()) {
           boolean result = compareAndSet(1, 0);
           if (result) {
               Thread thread = concurrentLinkedDeque.getFirst();
               // 公平锁唤醒阻塞的第一个线程
               LockSupport.unpark(thread);
               return true;
           }
       }
        return true;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/271706
推荐阅读
相关标签
  

闽ICP备14008679号