信号量:我在哪里可以了解基本概念,例如许可、公平、闯入等
developer.android.com 中的 Semaphore 类概述 看起来相当不错 - 对于那些已经熟悉概念和术语的人来说。
我熟悉其中的一些首字母缩略词和其他术语(例如 FIFO、锁定等),但也熟悉其他术语,例如 permits
、fairness
和 barging
对我来说是新的。
您能推荐一个好的在线资源来解释这些概念吗? (我大概可以弄清楚什么是许可和公平,但是闯入
目前还是未知数)。
编辑:收到下面的两个答案后,我意识到我需要刷新信号量(重新获取()术语)。我发现以下资源很有用:
- Semaphore_(programming)
- 信号量简介 作者: 理查德·霍尔博士
The Semaphore class overview in developer.android.com looks pretty good - for those who are already familiar with the concepts and terminology.
I am familiar with some of the acronyms and other jargon there (e.g. FIFO, lock, etc.) but others such as permits
, fairness
and barging
are new to me.
Can you recommend a good online source for explaining these concepts? (I can probably figure out what permits and fairness are but barging
is an unknown at this point).
EDIT: After receiving the two answers below, I realized that I need a refresh on semaphores (to re-acquire() terminology). I found the following resources to be useful:
- Semaphore_(programming)
- Introduction to Semaphores by
Dr. Richard S. Hall
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://download.oracle .com/javase/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html
http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Semaphore.html
这是摘录自被认为是 Java 并发领域的开创性著作之一的内容,您应该查看一下。
http://my.safaribooksonline.com/book/programming/java /0321349601/显式锁/287
http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html
http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Semaphore.html
This is an excerpt from what is considered one of the seminal works in java concurrency you should check it out.
http://my.safaribooksonline.com/book/programming/java/0321349601/explicit-locks/287
我自己没有遇到过这些,但我想我应该研究并总结我的发现,因为内嵌答案比外部链接更好(尽管,是的,OP是在推荐阅读之后):
允许是允许信号量保护代码的并发访问数。虽然信号量通常是简单的互斥体,但有时需要有多个线程接触代码。这类似于给呼叫中心打电话,其中一个电话号码连接到 8 条线路/运营商。
公平是指按照严格的先请求者顺序向请求者提供信号量。继续与呼叫中心类比,这意味着等待队列是严格的 FIFO。
插入本质上是一种带外请求,它将线程置于信号量队列的顶部。类比是优先客户(或内部呼叫)进入呼叫中心队列的顶部,而不是等待轮到他们。
如果公平性和闯入均未指定,则根据上下文切换的时间,授予对最近请求的访问权限符合规范。 “电话”就像打给公司总机/接待处的电话,即使呼叫处于等待应答状态,您也可能会幸运地在一个呼叫结束和另一个呼叫取消保持之间响铃。
如果我弄错了,请通过评论告诉我,我会修正/修改我的答案。
Hadn't come across these myself, but thought I'd research and summarise my findings as it's better to in-line answers than link externally (although, yes, the OP is after recommending reading):
permits are the number of concurrent accesses allowed to the semaphore-protected code. Although often semaphores are simple Mutex's, it is sometimes desirable to have more than one thread touching code. This is similar to phoning a call-centre, where there's one phone number connected to 8 lines/operators.
fairness is when a semaphore is made available to requesters in strict order of who requested first. Staying with the call-centre analogy, this means the on-hold queue is a strict FIFO.
barging is essentially an out-of-band request, that puts a thread to the top of the queue for a semaphore. The analogy is where preferred customers (or internal calls) go to the top of a call-centre queue, rather than waiting their turn.
If neither fairness nor barging are specified, then it's within spec to grant access to the most recent request, depending on timing of context switches. The 'phone analogy is a call to a company switchboard/reception, where even if calls are on hold waiting for answer, you may get lucky and ring between one call ending and another call being taken off-hold.
Let me know through comments if I've got this wrong, and I'll fix / cw my answer.