> Wesley Hall <noreply@example.com> wrote or quoted in
> Message-ID: <45677b9b$0$8718$ed2619ec@ptn-nntp-reader02.plus.net>:
[quoted text clipped - 13 lines]
> It seems that the ownership of lock is not helpful to the
> implementation of restricting the number of threads.
It is very helpful in solving the problem you originally proposed. The
issue was (as I understand it) that you might have more calls to release
permits than permits available, resulting in a total number of permits
higher than the initial figure, but this is expected behaviour in
Semaphore.
By maintaining account of lock ownership, you would be able to prevent
threads that had not previously requested a permit, from successfully
calling release. The number of permits would not be able to grow beyond
the initial figure because you are keeping close account of where the
permits currently are. Semaphore does not do this, requesting permits
and releasing permits are 'loosely coupled' and intentionally so.
> Let's consider the following case.
>
[quoted text clipped - 3 lines]
> - She is informed of when all the children finish their tasks.
> - Resource R0, R1, ... , Rn // ex: n > 100
If you intention is to inform a 'parent thread' that all 'child' threads
have finished their tasks, and you know the exact number of child thread
that will run, then you are using the wrong class, semaphore is not for you.
You might want to try CountDownLatch. The first paragraph of the JavaDoc
for this class reads...
"A synchronization aid that allows one or more threads to wait until a
set of operations being performed in other threads completes."
<snip code example>
I am not exactly sure what you are getting at from this point, and the
code example wasn't very clear to me. I am sorry, I cannot comment on that.
Red Orchid - 25 Nov 2006 02:51 GMT
Wesley Hall <noreply@example.com> wrote or quoted in
Message-ID: <4567a58d$0$8739$ed2619ec@ptn-nntp-reader02.plus.net>:
> <snip code example>
>
> I am not exactly sure what you are getting at from this point, and the
> code example wasn't very clear to me. I am sorry, I cannot comment on that.
With the current "java.util.concurrent.Semaphore",
The implementation of the case will be like:
<code_1>
....
class ResourceSupplier {
int MAX_AVAILABLE = 3;
Semaphore sema = new Semaphore(MAX_AVAILABLE);
Resource offer() {
sema.acquire();
Resource R = queue.offer();
if (R != null) {
return R;
}
// Code_Bock_A
sema.acquire(MAX_AVAILABLE - 1)
// All the resource are processed.
return null;
}
void processed() {
sema.release();
}
}
</code_1>
If a bug of calling "processed()" occur somewhere in the entire source,
the max available value will become 3 + x and "offer()" will be blocked
endlessly by "sema.acquire(MAX_AVAILABLE - 1)".
In multithreaded environment, the debugging can be very difficult.
Thank for your comments.
Wesley Hall - 25 Nov 2006 03:15 GMT
> If a bug of calling "processed()" occur somewhere in the entire source,
> the max available value will become 3 + x and "offer()" will be blocked
> endlessly by "sema.acquire(MAX_AVAILABLE - 1)".
...but aren't you just saying... "Look... here is some buggy code where
I am use a API class improperly... and there is a bug!"
The Semaphore class is very clear on how it works, the documentation
states the behaviour in VERY precise terms. If you do not use the
Semaphore class in the way it was intended, of course you will have
bugs. I could easily ask you to pick an API class at random, and I could
write some code that would use that API class incorrectly and there
would be a bug. This doesn't mean the API class itself has bugs.
It seems to me that what you are saying is that you find the Semaphore
class unintuative. That is a different issue, and is fair enough. It
might even be fair to say the majority of developers may find the class
unintuative. That does not make it 'buggy' in the strictest sense.