Should java/nio/channels/spi/AbstractInterruptibleChannel.java#isOpen be synchronized ?

Chris Hegarty chris.hegarty at oracle.com
Tue Dec 4 06:38:19 PST 2012


On 12/04/2012 02:13 PM, Alan Bateman wrote:
> On 04/12/2012 10:53, Sean Chou wrote:
>> Hello,
>>
>> I found AbstractInterruptibleChannel.close() is synchronized using
>> closeLock, but isOpen() is not synchronized. Javadoc of isOpen() says
>> "Returns: true if, and only if, this channel is open". I want to know
>> if isOpen() method is designed not to synchronize with closeLock ?
>> Because in close() method the closeLock embraces "open = false;" and "
>> implCloseChannel(); " , which looks like the open variable should
>> always reflect the real state.
> What problem are you running into? I see anything wrong with isOpen and
> it would create a lot of issues if it were to synchronize on closeLock.

I also don't see any issue. Since 'open' is volatile, visibility is 
guaranteed. Yes, this is racy code, but that's the nature of 
asynchronous close/interrupt.

-Chris.

>
> -Alan


More information about the nio-dev mailing list