R
Red Orchid
(Reference: http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Semaphore.html)
This is a part of the above page.
"...
Semaphores are often used to restrict the number of threads
than can access some (physical or logical) resource. ...
class Pool {
private static final MAX_AVAILABLE = 100;
private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);
public Object getItem() throws InterruptedException {
available.acquire();
return getNextAvailableItem();
}
public void putItem(Object x) {
if (markAsUnused(x))
available.release();
}
... "
In the above code,
the semaphore "available" do not guarantee reliably
that "MAX_AVAILABLE" is 100.
Because "Semaphore" allows the following code.
<code>
int val = 2;
Semaphore sema = new Semaphore(val);
sema.release();
// sema.availablePermits() is 3.
</code>
The max number of permits available is controlled by
the calling number of "release()" too.
If a bug occur in connection with calling "release()",
"MAX_AVAILABLE" will become 100 + x.
I think that the current design of "Semaphore" is brittle in
a use of restricting the number of threads than can access
some resource.
What is your comment ?
Thanks.
This is a part of the above page.
"...
Semaphores are often used to restrict the number of threads
than can access some (physical or logical) resource. ...
class Pool {
private static final MAX_AVAILABLE = 100;
private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);
public Object getItem() throws InterruptedException {
available.acquire();
return getNextAvailableItem();
}
public void putItem(Object x) {
if (markAsUnused(x))
available.release();
}
... "
In the above code,
the semaphore "available" do not guarantee reliably
that "MAX_AVAILABLE" is 100.
Because "Semaphore" allows the following code.
<code>
int val = 2;
Semaphore sema = new Semaphore(val);
sema.release();
// sema.availablePermits() is 3.
</code>
The max number of permits available is controlled by
the calling number of "release()" too.
If a bug occur in connection with calling "release()",
"MAX_AVAILABLE" will become 100 + x.
I think that the current design of "Semaphore" is brittle in
a use of restricting the number of threads than can access
some resource.
What is your comment ?
Thanks.