How and why can a Semaphore give out more permits than it was initialized with?

前端 未结 6 1369
庸人自扰
庸人自扰 2021-02-08 12:18

I am reading the book Java Concurrency in Practice. In a section about java.util.concurrent.Semaphore, the below lines are present in the book. It is a comment abou

6条回答
  •  没有蜡笔的小新
    2021-02-08 12:52

    I think that it means the times what we may require Semaphore as the times we released "extra" and plus the permits it created with.

    Such as:

    Semaphore s = new Semaphore(1); // one permit when initialize
    
    s.acquire();
    s.release();
    
    s.release(); // "extra" release.
    

    At this moment, this semaphore allows one permit originally and one "extra" permit

提交回复
热议问题