博客
关于我
ReentranLock源码分析
阅读量:265 次
发布时间:2019-03-01

本文共 2070 字,大约阅读时间需要 6 分钟。

ReentrantLock ?????????

ReentrantLock ? Java ?????????????????????????????????????????????????????????? AbstractQueuedSynchronizer (AQS) ?????CAS??????????????????

1. ??

ReentrantLock ??? Lock ?????? lock() ? unlock() ?????? newCondition() ????????????????????????????????????????????

2. ????

ReentrantLock ????????Sync?NonfairSync ? FairSync????NonfairSync ? FairSync ??? Sync?Sync ???? AbstractQueuedSynchronizer?

2.1 Sync ?

Sync ?? ReentrantLock ???????????????????????? nonfairTryAcquire()?tryRelease() ??

nonfairTryAcquire()

  • ????????????????
  • ????????state == 0????? CAS ??????
  • ????????????????????
  • ????????? false?

tryRelease()

  • ????????????????
  • ????????????? IllegalMonitorStateException?
  • ???????????????????
  • ???????????????

2.2 NonfairSync ?

NonfairSync ????????????????????? lock() ???? CAS ???????????????????????

acquire()

  • ??? nonfairTryAcquire() ????
  • ???????????????

addWaiter()

  • ???????????????
  • ?? CAS ???????????

acquireQueued()

  • ??????????????
  • ?????????? SIGNAL???????
  • ??????????????????

2.3 FairSync ?

FairSync ?????????? lock() ???? acquire(1) ????????????????????????????????????????????????

3. ????

3.1 ?????

public class TestReentrantLock {    private static Lock lock = new ReentrantLock();    public static void main(String[] args) {        MyThread t1 = new MyThread("t1", lock);        MyThread t2 = new MyThread("t2", lock);        MyThread t3 = new MyThread("t3", lock);        t1.start();        t2.start();        t3.start();    }    static class MyThread extends Thread {        private Lock lock;        MyThread(String name, Lock lock) {            super(name);            this.lock = lock;        }        public void run() {            lock.lock();            try {                System.out.println(Thread.currentThread().getName() + " running");                Thread.sleep(500);            } catch (InterruptedException e) {                e.printStackTrace();            } finally {                lock.unlock();            }        }    }}

3.2 ????

????????????????????????????????????

?????????????? ReentrantLock ?????????????????????

转载地址:http://qbtx.baihongyu.com/

你可能感兴趣的文章
Python tweepy写入到sqlite3 db
查看>>
Python TypeError:格式字符串的参数不足
查看>>
Python UI自动化测试Page Objects企业级实战
查看>>
python谷歌翻译,2021年9月10日亲测可用,一次可以翻译十万,强烈star
查看>>
Python UI自动化测试数据驱动实战
查看>>
Python UI自动化测试集成UnitTest
查看>>
Python UI自动化测试集成UnitTest
查看>>
python unicode-escape
查看>>
Python unittest mock:是否可以在测试时模拟方法默认参数的值?
查看>>
Python unittest单元测试框架 TestSuite测试套件
查看>>
PYTHON调离线语音合成并实时播放
查看>>
python unittest高级特性!
查看>>
Python unittest:如何将标准输出消息临时重定向到缓冲区并测试其内容?
查看>>
Python urllib/Requests下载文件失败,但浏览器下载失败
查看>>
Python urllib2 文件上传问题
查看>>
Python urllib2.open 连接由对等错误重置
查看>>
python urllib2详解及实例
查看>>
Python url请求提示certificate verify failed unable to get local issuer certificate
查看>>
Python UTC 日期时间对象的 ISO 格式不包括 Z(祖鲁语或零偏移)
查看>>
python valueerror object2_python遇到错误记录
查看>>