使用信号量来控制线程数量
如何利用 Semaphore 类来控制有权访问对象的线程数量?
How can I make use of Semaphore class in order to control number of threads that have an access to an object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用信号量控制对资源的访问的另一件好事是您可以在运行时调整信号量的大小。例如,您可以有一些用例,您希望允许更多用户基于某些业务逻辑访问资源,然后再次减少资源。
可调整大小信号量的示例代码
One more good thing about using semaphore to control access to resouce is you can resize your semaphore at runtime. For eg you can have some use case where you want to allow more user to access resource based on some business logic and then reduce it again.
Sample code for re sizable semaphore
这是如何使用信号量来限制对对象的并发访问的一个很好的示例:
http://technicalmumbojumbo.wordpress.com/2010/02/21/java-util-concurrent-java-5-semaphore/
关键点是:
This is a great example of how Semaphore can be used to limit concurrent access to an object:
http://technicalmumbojumbo.wordpress.com/2010/02/21/java-util-concurrent-java-5-semaphore/
The key points being:
只需使用:
javadoc 中有一个关于如何使用它的广泛示例:
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Semaphore.html
Just use:
there's an extensive example on how to use it in the javadoc:
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Semaphore.html
也许您可以阅读这个问题的答案,并查看此示例
Perhaps you could read the answer to this question, and have a look at this example