简体   繁体   English

java.concurrent.Semaphore的异常行为

[英]Unexpected behavior of java.concurrent.Semaphore

I am in a situation where I need to use Semaphore from the java.concurrent package so that I may be able to restrict the number of threads taking hold of a certain resource. 我处于需要使用java.concurrent包中的Semaphore的情况,以便可以限制占用某种资源的线程数。 Following is the very basic setup (code): 以下是非常基本的设置(代码):

    package semaphor;

    import java.util.concurrent.Semaphore;

    /**
     * Created by NawazishMohammad on 15-04-2015.
     */
    public class SemaphoreTester {
       public static void main (String [] args){
           new Thread(new MyRunnable(), "T1").start();
           new Thread(new MyRunnable(), "T2").start();
           new Thread(new MyRunnable(), "T3").start();
           new Thread(new MyRunnable(), "T4").start();
       }
    }

    class MyRunnable implements Runnable{

        @Override
        public void run() {
            semaphoreStatus();
        }

        public void semaphoreStatus(){
            System.out.println("Thread waiting for permit: "+Thread.currentThread().getName());
            Semaphore sem = new Semaphore(2);
            try {
                sem.acquire();
                System.out.println("Thread got permit: " + Thread.currentThread().getName());
                System.out.println("Thread releasing permit: "+Thread.currentThread().getName());
                sem.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }

}

I am getting the following output 我得到以下输出

Thread waiting for permit: T1
Thread waiting for permit: T2
Thread waiting for permit: T3
Thread waiting for permit: T4
Thread got permit: T3
Thread got permit: T4
Thread got permit: T2
Thread got permit: T1
Thread releasing permit: T2
Thread releasing permit: T4
Thread releasing permit: T3
Thread releasing permit: T1

Note that although I have instantiated Semaphore for total concurrent thread count of two " new Semaphore(2); ", implying that at a time only 2 threads can have access to a particular resource, I can find all 4 threads: T1-T4 having acquired Semaphore permit (and hence access to the restricted resource). 请注意,尽管我为两个“ new Semaphore(2); ”实例化了总并发线程数的new Semaphore(2); ,这意味着一次只有2个线程可以访问特定资源,但是我可以找到所有4个线程:T1-T4获得信号量许可(并因此访问受限资源)。 Could anyone please clarify my misunderstanding, if any, about java.concurrent.Semaphore since I do not expect the output: 任何人都可以澄清我对java.concurrent.Semaphore误解(如果有的话),因为我不期望输出:

Thread got permit: T3
Thread got permit: T4
Thread got permit: T2
Thread got permit: T1 

I understand that Semaphore, at any instance, cannot tender more than "2" permits (for this example) to any number of threads that might be waiting. 我知道信号量在任何情况下都不能将“ 2”个许可(对于本示例)提供给可能正在等待的任何数量的线程。

You are creating a new semaphore in each thread, meaning that each thread will see a different Semaphore. 您正在每个线程中创建一个新的信号量,这意味着每个线程将看到不同的信号量。 Instead of calling Semaphore sem = new Semaphore(2); 而不是调用Semaphore sem = new Semaphore(2); inside semaphoreStatus() , you should construct the Semaphore outside of the threads and then share it with them. semaphoreStatus()内部,您应该在线程外部构造Semaphore,然后与线程共享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM