issue with ServerSocket::accept
David Daney
ddaney@avtrex.com
Fri May 14 20:18:00 GMT 2004
Bryce McKinlay wrote:
.
.
.
>>> Well maybe accept doesn't return when the socket is closed. Perhaps
>> each thread entering ServerSocket.accept() should put itself on a
>> list of blocked acceptors. Then when close() is called each thread
>> on the list is interrupted...
>>>> The threads would have to synchronize when adding themselves to the
> list. Are multiple threads allowed to call accept() on the same
> socket? If that is not valid, then the implementation ought to be
> pretty simple - we can just add a thread field to each socket. We just
> have to watch out for race conditions in the signaling mechanism.
>It is my understanding that multiple threads can be in accept() on the
same socket. When a connection arrives, the OS chooses one of waiting
threads and its accept() returns. That is not to say that it is
necessarily a good idea, but I think it is legal.
The accepting thread synchronizes on putting itself on the list and
taking itself off after the accept(2) returns. The closing thread does
the close(2) and interrupting the accepting threads in a block
synchronized on the same list.
By the time that the accepting thread leaves the synchronized block
taking itself off the list, it will either have succeeded or been
interrupted. But it will never have been interrupted after leaving the
second synchronized block.
At least that's how I see it.
David Daney
More information about the Java
mailing list