同步锁授予命令何时需要多个线程呢? [英] synchronized lock grant order when multiple thread ask for it?

查看:71
本文介绍了同步锁授予命令何时需要多个线程呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当多个线程尝试获取锁时,如果一个线程已经获取了锁,则该锁处于同步状态. 我的理解是,锁定将按照获取锁定请求的顺序给出.

When multiple thread try to acquire the lock on synchronized when lock is already acquired by one thread. My understanding was that lock will be given in order of acquire lock request.

但是根据O'Reilly的书,将给出最适合平台的Java线程锁定.多数民众赞成在非常抽象的说法.我认为平台主要是指操作系统. 我的问题是什么标准基于哪个JVM决定最适合平台的内容以及开发人员如何在此基础上进行核算 做编程吗?

But as per the book O'Reilly Java threads lock will be given that is best for platform. Thats very abstract statement.I think platform mainly meant OS here. My question what is the criteria based on which JVM decides what is best for platform and how developer accounts it while doing programming?

更新:-我知道我可以使用带有公平参数的Lock对象.但是只是想知道它如何与同步锁一起工作?

Update:- i know i can use Lock object with fairness argument. But just want to know how does it work with synchronized locks?

推荐答案

synchronized的获取遵循non-fair锁定策略.也就是说,在阻塞时首先进入的线程可能不是第一个获取的线程.如果您想使用fair锁,请使用new ReentrantLock(true)

synchronized acquisition follows a non-fair lock policy. That is, threads that enter first while blocking may not be the first to acquire. If you want a fair lock use a new ReentrantLock(true)

这篇关于同步锁授予命令何时需要多个线程呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆