Async channels

Alan Bateman Alan.Bateman at Sun.COM
Thu Apr 23 11:34:22 PDT 2009


Rémi Forax wrote:
> :
> that why I don't understand why a AsyncChannelGroup (perhaps a 
> different implementation)
> can't be used as a group for AsyncFileChannels.
The issue is trying to share resources and a thread pool between 
potentially two different mechanisms. It may not be an issue in some 
cases but in others it may be very problematic. For example, if you used 
a fixed thread pool then an implementation may be forced to divide the 
pool into two with some threads servicing I/O events for the network 
channels, and the others for file I/O. One solution would be to prohibit 
mixing channels in a group but we don't want to overly complicate the API.

> :
> complexity == 1 boolean that already exist in the implementation.
>
> public AsynchronousFileChannel newAsynchronousFileChannel(Path path,
>                                                              Set<? 
> extends OpenOption> options,
>                                                              
> ExecutorService executor,
>                                                              boolean 
> shared,                                <--- shutdown on close if not 
> shared !
>                                                              
> FileAttribute<?>... attrs)
That would work except that it might leak resources, consider:

ExecutorService pool = ...
ch1 = AsynchronousFileChannel.open(... pool, shared=true)
ch2 = AsynchronousFileChannel.open(... pool, shared=true)
ch1.close
ch2.close
pool.shutdown

The channels are closed and the thread pool is shutdown but the I/O 
machinery may still exit and may have threads waiting on I/O events.

However your suggestion leads to another idea that is worth exploring. 
The other idea is to simply allow AsynchronousFileChannel share a thread 
pool and to terminate the I/O machinery when the last channel using the 
pool is closed. In implementation terms it's just a mapping from pool to 
a hidden group that is created when a mapping doesn't initially exist. 
The only downside that comes to mind is that it is inconsistent with how 
we share resources between network channels. That might not be too much 
of a concern because this is only something for a small set of cases.

-Alan.





More information about the nio-dev mailing list