加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

关于多次锁定尝试,使用java.concurrent.ReentrantLock

发布时间:2020-12-15 02:00:15 所属栏目:Java 来源:网络整理
导读:我注意到以下代码块: final Lock s = new ReentrantLock(); for(int i = 0 ; i 1000 ; i++) { s.lock(); System.out.println(i+" :" +s.tryLock()+" "); } 印刷品: 0 :true 1 :true 2 :true 3 :true ... 这很奇怪 – 我希望连续的锁失败,因为s永远不会被解
我注意到以下代码块:

final Lock s = new ReentrantLock();
    for(int i = 0 ; i < 1000 ; i++)
    {
        s.lock();
        System.out.println(i+" :" +s.tryLock()+" ");
    }

印刷品:

0 :true 
1 :true 
2 :true 
3 :true 
...

这很奇怪 – 我希望连续的锁失败,因为s永远不会被解锁.

这里有什么不好意思吗?

解决方法

Javadoc is your friend.你真的应该读它.

来自:ReentrantLock.lock()

If the current thread already holds the lock then the hold count is incremented by one and the method returns immediately.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读