使用信号量来控制线程数量

发布于 2024-10-21 13:27:39 字数 38 浏览 2 评论 0原文

如何利用 Semaphore 类来控制有权访问对象的线程数量?

How can I make use of Semaphore class in order to control number of threads that have an access to an object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

阪姬 2024-10-28 13:27:40
  • 使用允许的最大线程数初始化信号量,
  • 如果线程进入限制区域,则将信号量计数器减一;
  • 如果线程离开限制区域,则将信号量计数器加一
  • Initialize the Semaphore with the max number of alowed threds,
  • reduce the semaphore counter by one if a thread enters the restricted area
  • increse the semaphore counter by one if the thred leaves the restricted area
清欢 2024-10-28 13:27:40

使用信号量控制对资源的访问的另一件好事是您可以在运行时调整信号量的大小。例如,您可以有一些用例,您希望允许更多用户基于某些业务逻辑访问资源,然后再次减少资源。
可调整大小信号量的示例代码

public class ResizeableSemaphore extends Semaphore
{

    private static final long serialVersionUID = 1L;
    private int permit;
    public ResizeableSemaphore(int permit) {
        super(permit);
        this.permit=permit;
    }       

    public synchronized void resizeIfRequired(int newPermit)
    {
        int delta = newPermit - permit;
        if(delta==0) return;
        if(delta > permit) this.release(delta); // this will increase capacity
        if(delta < 0) this.reducePermits(Math.abs(delta));
        this.permit=newPermit;
    }   
}

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

public class ResizeableSemaphore extends Semaphore
{

    private static final long serialVersionUID = 1L;
    private int permit;
    public ResizeableSemaphore(int permit) {
        super(permit);
        this.permit=permit;
    }       

    public synchronized void resizeIfRequired(int newPermit)
    {
        int delta = newPermit - permit;
        if(delta==0) return;
        if(delta > permit) this.release(delta); // this will increase capacity
        if(delta < 0) this.reducePermits(Math.abs(delta));
        this.permit=newPermit;
    }   
}
孤单情人 2024-10-28 13:27:40

这是如何使用信号量来限制对对象的并发访问的一个很好的示例:

http://technicalmumbojumbo.wordpress.com/2010/02/21/java-util-concurrent-java-5-semaphore/

关键点是:

  • 当您构造信号量,可以声明最大并发数(即允许同时访问的线程数),
  • 要求每个线程都尝试acquire()信号量;让信号量跟踪并发访问

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:

  • When you construct the Semaphore, you can declare the max concurrency (i.e., number of threads allowed to access concurrently)
  • You require each thread to attempt to acquire() the Semaphore; let the Semaphore keep track of concurrent access
夜未央樱花落 2024-10-28 13:27:40

只需使用:

java.util.concurrent.Semaphore

javadoc 中有一个关于如何使用它的广泛示例:

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Semaphore.html

Just use:

java.util.concurrent.Semaphore

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

执手闯天涯 2024-10-28 13:27:40

也许您可以阅读这个问题的答案,并查看此示例

Perhaps you could read the answer to this question, and have a look at this example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文