> Hello.
>
[quoted text clipped - 35 lines]
> (number of connections went down from 4) in order to resume working.
> What's the proper way to go about it?
>> while (true)
>> {
[quoted text clipped - 7 lines]
>> // a notify()cation is issued
>> }
I assume you have placed this in a synchronized (this) block (or
synchronized method). Also presumably the client threads finish with
something like:
synchronized (this) { // same this as above
this.notify();
--connections;
}
You may notice a problem in that unless the limit is reached, client
threads fail to exit. They cannot enter the synchronized block until the
server is in wait (which releases the lock).
> If my memory doesn't fail me this is a typical application case for a
> semaphore.
>
> http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Semaphore.html
Yup. Which will probably be faster, because there is no need to acquire
a lock in the usual case. Not that it matters in this case.
> There might also be a socket based solution (i.e. a restriction) not sure.
The operating system will probably set a limit on the total number of
connections.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Ney André de Mello Zunino - 31 Mar 2006 02:31 GMT
[...]
> You may notice a problem in that unless the limit is reached, client
> threads fail to exit. They cannot enter the synchronized block until the
> server is in wait (which releases the lock).
Thank you for your post. I was just struggling with the very thing you
describe here. I had prepared a synchronized method named
clientConnectionTerminated() to handle the updating of the connection
count and to notify the server that it could again listen for incoming
connection requests. That method would be called by a connection handler
object when the connection would be terminated by the client.
The problem, as you have explained, is that in the case where the
maximum number of connections had not yet been reached, the connection
handler would block on the call to the clientConnectionTerminated()
method, as the Server would not yet have given up the lock on its
monitor. Have I got it right?
>> If my memory doesn't fail me this is a typical application case for a
>> semaphore.
[quoted text clipped - 3 lines]
> Yup. Which will probably be faster, because there is no need to acquire
> a lock in the usual case. Not that it matters in this case.
I guess this was a typical situation of choosing the wrong tool for the
job. I will indeed consider using semaphores. Just a quick question: I
had someone tell me that they were not actually part of the standard
library, but some kind of non-official add-on facility. Is that still
(if it's ever been) true? May I safely use it in my projects without
compromising portability and compatibility?
Thank you,

Signature
Ney André de Mello Zunino
Graduando em Ciência da Computação
Universidade Federal de Santa Catarina