Java ReentrantLock 详解

ReentrantLock 和 synchronized 的区别:

1.ReentrantLock 不需要代码块,在开始的时候lock,在结束的时候unlock 就可以了。

但是要注意,lock 是有次数的,如果连续调用了两次lock,那么就需要调用两次unlock。否则的话,别的线程是拿不到锁的。

    /**
     * 很平常的用锁 没有感觉reentrantlocak 的强大
     *
     * 如果当前线程已经拿到一个锁了 那么再次调用 会里面返回true
     * 当前线程的锁数量是 2个
     *
    */
    private void normalUse() {
        ReentrantLock reentrantLock =  new ReentrantLock();
        new Thread("thread1"){
            @Override
            public void run() {
                super.run();
                LogToFile.log(TAG,"thread1 lock");
                reentrantLock.lock();
                reentrantLock.lock();
               
                LogToFile.log(TAG,"thread1 getHoldCount:"  + reentrantLock.getHoldCount() );

                try {
                    LogToFile.log(TAG,"thread1 run");

                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                 //如果说你的线程占了两个锁 一个没有释放 其他线程都拿不到这个锁
                 //上面lock 了两次 这里需要unLock 两次
                reentrantLock.unlock();
                reentrantLock.unlock();
                LogToFile.log(TAG,"thread1 unlock");
            }
        }.start();


        new Thread("thread2"){
            @Override
            public void run() {
                super.run();

                LogToFile.log(TAG,"thread2 lock");
                reentrantLock.lock();
                LogToFile.log(TAG,"thread2 run");

                reentrantLock.unlock();
                LogToFile.log(TAG,"thread2 unlock");

            }
        }.start();
    }

ReentrantLock trylock()

ReentrantLock trylock 会尝试去拿锁,如果拿得到,就返回true, 如果拿不到就返回false.这个都是立马返回,不会阻塞线程。
如果当前线程已经拿到锁了,那么trylock 会返回true.

    /**
     * trylock 不管能不能拿到锁 都会里面返回结果 而不是等待
     * tryLock 会破坏ReentrantLock 的 公平机制
     * 注意 如果没有拿到锁 执行完代码之后不要释放锁,
     * 否则会有exception
     *
     */
    private void UseTryLock() {
        ReentrantLock reentrantLock =  new ReentrantLock();
        new Thread("thread1"){
            @Override
            public void run() {
                super.run();
                LogToFile.log(TAG,"thread1 lock");
                reentrantLock.lock();
                try {
                    LogToFile.log(TAG,"thread1 run");

                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                reentrantLock.unlock();
                LogToFile.log(TAG,"thread1 unlock");
            }
        }.start();


        new Thread("thread2"){
            @Override
            public void run() {
                super.run();

                LogToFile.log(TAG,"thread2 tryLock");
                boolean success = reentrantLock.tryLock();
                LogToFile.log(TAG,"thread2 run");

                if (success) {
                    reentrantLock.unlock();
                    LogToFile.log(TAG,"thread2 unlock");
                    //    java.lang.IllegalMonitorStateException
                    //        at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:123)
                    //        at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1235)
                    //        at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:429)

                }

            }
        }.start();
    }

ReentrantLock .tryLock(5, TimeUnit.SECONDS)

ReentrantLock 可以指定,尝试去获取锁,等待多长时间之后,如果还获取不到,那么就放弃。

    /**
     * tryLock(5, TimeUnit.SECONDS)
     * 如果5s之后 还拿不到锁  那么就不再堵塞线程,也不去拿锁了 执行执行下面的代码了
     */
    private void UseTryLockWhitTime() {
        ReentrantLock reentrantLock =  new ReentrantLock();
        new Thread("thread1"){
            @Override
            public void run() {
                super.run();
                LogToFile.log(TAG,"thread1 lock");
                reentrantLock.lock();
                try {
                    LogToFile.log(TAG,"thread1 run");

                    Thread.sleep(1000 * 10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                reentrantLock.unlock();
                LogToFile.log(TAG,"thread1 unlock");
            }
        }.start();


        new Thread("thread2"){
            @Override
            public void run() {
                super.run();

                LogToFile.log(TAG,"thread2 tryLock");
                boolean success = false;
                try {
                    success = reentrantLock.tryLock(5, TimeUnit.SECONDS);
                    LogToFile.log(TAG,"thread2 run");

                    if (success) {
                        reentrantLock.unlock();
                        LogToFile.log(TAG,"thread2 unlock");
                        //    java.lang.IllegalMonitorStateException
                        //        at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:123)
                        //        at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1235)
                        //        at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:429)

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

ReentrantLock .lock();

一旦调用了ReentrantLock .lock() ,如果你的线程被其他线程interrupt,那么你的线程并不会interrupt,而是继续等待锁。

    /**
     * lockInterruptibly() 和  lock 的区别是
     * 如果当前线程被Interrupt 了,那么lockInterruptibly 就不再等待锁了。直接会抛出来一个异常给你处理
     * 如果是lock 的话 就算 线程被其他的线程lockInterrupt 了,不管用,他还会继续等待锁
     */
    private void UseLockIntercepter() {
        ReentrantLock reentrantLock =  new ReentrantLock();
        Thread thread = new Thread("thread1") {
            @Override
            public void run() {
                super.run();
                LogToFile.log(TAG, "thread1 lock");
                reentrantLock.lock();
                LogToFile.log(TAG, "thread1 run");
                if (reentrantLock.getHoldCount() != 0) {
                    reentrantLock.unlock();
                }
                LogToFile.log(TAG, "thread1 unlock");
            }
        };


        new Thread("thread2"){
            @Override
            public void run() {
                super.run();

                LogToFile.log(TAG,"thread2 tryLock");
                boolean success = false;
                try {
                    reentrantLock.lock();
                    thread.start();

                    thread.interrupt();
                    success = reentrantLock.tryLock(5, TimeUnit.SECONDS);
                    LogToFile.log(TAG,"thread2 run");

                    if (success) {
                        reentrantLock.unlock();
                        reentrantLock.unlock();
                        LogToFile.log(TAG,"thread2 unlock");
                        //    java.lang.IllegalMonitorStateException
                        //        at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:123)
                        //        at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1235)
                        //        at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:429)

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

ReentrantLock .lockInterruptibly();

如果当前线程调用lockInterruptibly 这个方法去获取锁,当其他的线程把你的线程给interrupt 之后,那么你的线程就不再等待锁了,执行下面的代码。

    /**
     * lockInterruptibly() 和  lock 的区别是
     * 如果当前线程被Interrupt 了,那么lockInterruptibly 就不再等待锁了。直接会抛出来一个异常给你处理
     * 如果是lock 的话 就算 线程被其他的线程lockInterrupt 了,不管用,他还会继续等待锁
     */
    private void UseLockIntercepter2() {
        ReentrantLock reentrantLock =  new ReentrantLock();
        Thread thread = new Thread("thread1") {
            @Override
            public void run() {
                super.run();
                LogToFile.log(TAG, "thread1 lock");
                try {
                    reentrantLock.lockInterruptibly();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    LogToFile.log(TAG, "thread1 lock InterruptedException ");

                }
                LogToFile.log(TAG, "thread1 run");
                if (reentrantLock.getHoldCount() != 0) {
                    reentrantLock.unlock();
                }
                LogToFile.log(TAG, "thread1 unlock");
            }
        };


        new Thread("thread2"){
            @Override
            public void run() {
                super.run();

                LogToFile.log(TAG,"thread2 tryLock");
                boolean success = false;
                try {
                    reentrantLock.lock();
                    thread.start();

                    thread.interrupt();
                    success = reentrantLock.tryLock(5, TimeUnit.SECONDS);
                    LogToFile.log(TAG,"thread2 run");

                    if (success) {
                        reentrantLock.unlock();
                        reentrantLock.unlock();
                        LogToFile.log(TAG,"thread2 unlock");
                        //    java.lang.IllegalMonitorStateException
                        //        at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:123)
                        //        at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1235)
                        //        at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:429)

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

ReentrantLock newCondition()使用:

condition. 的await 相当于 object 的wait 方法,就是等待其他线程唤醒,但是调用之前必须要reentrantLock.lock(); 一下,不然会抛异常。

    /**
     * tryLock(5, TimeUnit.SECONDS)
     * 如果5s之后 还拿不到锁  那么就不再堵塞线程,也不去拿锁了 执行执行下面的代码了
     */
    private void UseCondition() {
        ReentrantLock reentrantLock =  new ReentrantLock();
        Condition condition = reentrantLock.newCondition();
        Thread thread = new Thread("thread1") {
            @Override
            public void run() {
                super.run();

                try {
                    reentrantLock.lock();
                    LogToFile.log(TAG, "thread1 wait");
                    //await 其实是会释放锁的 其他的线程可以拿到锁 然后signal  不然的话岂不是要卡死?
                    condition.await();

                    LogToFile.log(TAG, "thread1 run");

                    Thread.sleep(1000 * 3);
                    condition.signalAll();
                    reentrantLock.unlock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LogToFile.log(TAG, "thread1 unlock");
            }
        };
        thread.start();


        Thread thread1 = new Thread("thread2") {
            @Override
            public void run() {
                super.run();
                //调用signal 之前一定要lock
                reentrantLock.lock();
                //就算没有人调用await 那么signal 方法也不会有什么问题
                condition.signal();

                try {
                    LogToFile.log(TAG, "thread2 wait");
                    condition.await();
                    LogToFile.log(TAG, "thread2 run");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                reentrantLock.unlock();
            }
        };
        thread1.start();
    }

Object wait方法使用:

wait 方法调用之前也要synchronized 一个锁。

    /**
     * 使用wait
     */
    private void UseWait() {
        ReentrantLock reentrantLock =  new ReentrantLock();
        Condition condition = reentrantLock.newCondition();
        new Thread("thread1"){
            @Override
            public void run() {
                super.run();

                try {
                    synchronized (condition) {
                        LogToFile.log(TAG,"thread1 wait");
                        condition.wait();
                    }

                    LogToFile.log(TAG,"thread1 run");

                    Thread.sleep(1000 * 10);
                    synchronized (condition) {
                        condition.notifyAll();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LogToFile.log(TAG,"thread1 unlock");
            }
        }.start();


        new Thread("thread2"){
            @Override
            public void run() {
                super.run();
                synchronized (condition) {
                    condition.notify();

                }
                try {
                    LogToFile.log(TAG,"thread2 wait");
                    synchronized (condition){
                        condition.wait();
                    }
                    LogToFile.log(TAG,"thread2 run");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

synchronized

synchronized 锁等待的线程,当被其他线程interrupt 的时候,并不会停止。

/**
 * synchronized 的锁,不能被中断
 */
public void testSyncInter(){
    Object lock = new Object();
    Thread thread = new Thread("thread1") {
        @Override
        public void run() {
            super.run();
            synchronized (lock) {
                LogToFile.log(TAG, "thread1 lock");
                LogToFile.log(TAG, "thread1 run");
            }
            LogToFile.log(TAG, "thread1 unlock");

        }
    };


    new Thread("thread2"){
        @Override
        public void run() {
            super.run();
            synchronized (lock) {
                LogToFile.log(TAG,"thread2 start");
                thread.start();
                thread.interrupt();

            }
            LogToFile.log(TAG,"thread2 run");
        }
    }.start();
}

BlockingQueue 就是使用ReentrantLock 实现的阻塞队列:

    /**
     * BlockingQueue  会阻塞当前的线程  等着其他线程放数据
     */
    public void testArrayBlockQueue(){
        BlockingQueue blockingQueue = new ArrayBlockingQueue<>(4);

        new Thread("thread1"){
            @Override
            public void run() {
                super.run();
                try {
                    LogToFile.log(TAG,"thread1 take");
                    blockingQueue.take();
                    LogToFile.log(TAG,"thread1 take  get");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread("thread2"){
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    LogToFile.log(TAG,"thread2 put");

                    //put 会阻塞 但是add 不会
                    blockingQueue.put(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

ArrayBlockingQueue 源码解析:

下面的代码,可以看到ArrayBlockingQueue 创建了ReentrantLock ,以及两个condition


    /** Main lock guarding all access */
    final ReentrantLock lock;

    /** Condition for waiting takes */
    private final Condition notEmpty;

    /** Condition for waiting puts */
    private final Condition notFull;

public ArrayBlockingQueue(int capacity, boolean fair) {
        if (capacity <= 0)
            throw new IllegalArgumentException();
        this.items = new Object[capacity];
        lock = new ReentrantLock(fair);
        notEmpty = lock.newCondition();
        notFull =  lock.newCondition();
    }

当我们往里面放元素的时候,我们会对notFull 进行等待,因为队列不是空的了。

当出队列的时候,notFull.signal(); 也就是当前的队列不是满的了,可以放元素进去了。

    /**
     * Inserts the specified element at the tail of this queue, waiting
     * for space to become available if the queue is full.
     *
     * @throws InterruptedException {@inheritDoc}
     * @throws NullPointerException {@inheritDoc}
     */
    public void put(E e) throws InterruptedException {
        checkNotNull(e);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == items.length)
                notFull.await();
            enqueue(e);
        } finally {
            lock.unlock();
        }
    }

    private E dequeue() {
        // assert lock.getHoldCount() == 1;
        // assert items[takeIndex] != null;
        final Object[] items = this.items;
        @SuppressWarnings("unchecked")
        E x = (E) items[takeIndex];
        items[takeIndex] = null;
        if (++takeIndex == items.length)
            takeIndex = 0;
        count--;
        if (itrs != null)
            itrs.elementDequeued();
        notFull.signal();
        return x;
    }

当我们往里面取元素的时候,如果队列是空的了,那么notEmpty 就要等待,当enqueue一个元素的时候,notEmpty 不是空就唤醒,那么take 就可以执行去拿元素了。

    public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == 0)
                notEmpty.await();
            return dequeue();
        } finally {
            lock.unlock();
        }
    }

    private void enqueue(E x) {
        // assert lock.getHoldCount() == 1;
        // assert items[putIndex] == null;
        final Object[] items = this.items;
        items[putIndex] = x;
        if (++putIndex == items.length)
            putIndex = 0;
        count++;
        notEmpty.signal();
    }

你可能感兴趣的:(Android,java)