重写一个锁的类里面包含内部类(非公共)继承AQS*

public class MyLock2 implements Lock{
    
    private Helper helper=new Helper();

    @Override
    public void lock() {
        helper.acquire(1);
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {
        helper.acquireInterruptibly(1);
    }

    @Override
    public boolean tryLock() {
        return helper.tryAcquire(1);
    }

    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return helper.tryAcquireSharedNanos(1,unit.toNanos(time));
    }

    @Override
    public void unlock() {
        helper.release(1);
    }

    @Override
    public Condition newCondition() {
        return helper.newCondition();
    }

    private class Helper extends AbstractQueuedSynchronizer{
        //如果第一个线程拿到锁返回true
        //如果第二个线程拿不到锁返回false
        //如何判断是第一个线程进来还是其他线程进来?
        @Override
        protected boolean tryAcquire(int arg) {
            int state=getState();
            if(state==0){
                if(compareAndSetState(0,arg)){
                    setExclusiveOwnerThread(Thread.currentThread());
                    return true;
                }
            }
            return false;
        }

        @Override
        protected boolean tryRelease(int arg) {
            if(Thread.currentThread()!=getExclusiveOwnerThread()){
                throw new RuntimeException();
            }
            int state=getState()-arg;
            boolean flag=false;
            if(state==0){
                setExclusiveOwnerThread(null);
                flag=true;
            }
            setState(state);
            return flag;
        }

        protected Condition newCondition(){
            return new ConditionObject();
        }
    }
}

测试是否存在线程安全性问题

public class Main {
    private  int value;
    private MyLock2 myLock2=new MyLock2();
    public int getValue(){
        myLock2.lock();
        try {
            Thread.sleep(1000);
            return value++;
        } catch (InterruptedException e) {
            throw new RuntimeException();
        }finally {
            myLock2.unlock();
        }
    }

    public static void main(String[] args){
        Main m=new Main();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println(Thread.currentThread().getName()+"得到的结果>>>>>>"+m.getValue());
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println(Thread.currentThread().getName()+"得到的结果>>>>>>"+m.getValue());
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println(Thread.currentThread().getName()+"得到的结果>>>>>>"+m.getValue());
                }
            }
        }).start();
    }
}

Yfangliang
0 声望1 粉丝