JUC - ReentrantLock源码解析 - 多图警告

x33g5p2x  于2022-02-12 转载在 其他  
字(2.8k)|赞(0)|评价(0)|浏览(355)

ReentrantLock分类FairSync公平锁 - 构造函数true - 线程轮流获得锁NonfairSync非公平锁 - 默认,构造函数false - 跟Synchronized一样是非公平

2.1.1.0 构造函数

2.1.1.1 lockInterruptibly - 可中断锁 - 正在等待获取锁的线程可直接调用Thread.interrupt该线程直接放弃获取锁,且直接抛出异常

ReentrantLock

AbstractQueuedSynchronizer

tryAcquire

NonfairSync

Sync

doAcquireInterruptibly

2.1.1.2 lock - 等待锁的线程在另一个线程被interrupt不会立刻终止的原因,只有获取到锁然后才会终止

2.1.1.3 unlock

Sync

2.1.1.5 如果你看懂了前面的代码解析,这段代码的输出你应该也能看懂

区别1. lockInterruptibly:等待获取锁的线程1,在此过程中,如果被其他线程nterrupt,则该线程1立刻直接不等待获取锁,丢弃锁,直接抛出异常2. lock:等待获取锁的线程1,在此过程中,如果被其他线程interrupt,则延迟到获取锁才interrupt

package top.linruchang.springdemo;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 作用:
 *
 * @author LinRuChang
 * @version 1.0
 * @date 2021/03/14
 * @since 1.8
 **/
@Slf4j
public class OtherTest3 {
    public static void main(String[] args) throws InterruptedException {

        System.out.println("\n=========LockInterruptibly===========\n");
        testLockInterruptibly();

        Thread.sleep(15000);

        System.out.println("\n==========Lock==========\n");
        testLock();

    }

    public static void testLockInterruptibly() throws InterruptedException {
        TestLock testLock = new TestLock();

        Thread thread1 = new Thread(() -> {
            testLock.testLockInterruptibly();
        });
        thread1.start();


        Thread.sleep(500);

        Thread thread2 = new Thread(() -> {
            testLock.testLockInterruptibly();
        });
        thread2.start();

        Thread.sleep(500);
        log.info("中断第二个线程");
        thread2.interrupt();

    }

    public static void testLock() throws InterruptedException {
        TestLock testLock = new TestLock();

        Thread thread1 = new Thread(() -> {
            testLock.testLock();
        });
        thread1.start();


        Thread.sleep(500);

        Thread thread2 = new Thread(() -> {
            testLock.testLock();
        });
        thread2.start();

        Thread.sleep(500);
        log.info("中断第二个线程");
        thread2.interrupt();

    }

}

@Slf4j
class TestLock {

    ReentrantLock reentrantLock = new ReentrantLock();

    public void testLockInterruptibly() {
        String threadName = Thread.currentThread().getName();
        log.info(threadName + "启动");
        try {
            //获取到锁,但可以调用interceprt使得锁失效
            reentrantLock.lockInterruptibly();
            log.info(threadName + ":休眠10s");
            Thread.sleep(10000);
            log.info(threadName + ":休眠起来啦");
        } catch (Exception e) {
            log.error(threadName + ":发生异常" + e);
        } finally {
            reentrantLock.unlock();
            log.info(threadName + "结束,并释放锁");
        }
    }

    public void testLock()  {
        String threadName = Thread.currentThread().getName();
        log.info(threadName + "启动");
        try {
            //获取到锁,但可以调用interceprt使得锁失效
            reentrantLock.lock();
            log.info(threadName + ":第一个休眠10s");
            try {
                Thread.sleep(10000);
            }catch (Exception e) {
                log.error(threadName + ":发生异常【第一个休眠】" + e);
            }
            log.info(threadName + ":第一个休眠起来啦");

            log.info(threadName + ":第二个休眠10s");
            try {
                Thread.sleep(10000);
            }catch (Exception e) {
                log.error(threadName + ":发生异常【第二个休眠】" + e);
            }
            log.info(threadName + ":第二个休眠起来啦");

        } catch (Exception e) {
            log.error(threadName + ":发生异常" + e);
        } finally {
            reentrantLock.unlock();
            log.info(threadName + "结束,并释放锁");
        }
    }
}

相关文章