Fwd: AutoCloseable blocking or not

David Holmes david.holmes at oracle.com
Fri Oct 5 11:10:53 UTC 2012


On 5/10/2012 7:56 PM, Alan Bateman wrote:
>
> Forwarding Kasper's mail to the right list.
>
> From: Kasper Nielsen <kasperni at gmail.com>
> To: nio-discuss at openjdk.java.net
>
> I have a question about the AutoCloseable interface. Since I cannot
> find any mention about how asynchronously closeable resources should
> be handled.
>
> Say I wanted juc.ThreadPoolExecutorService to implement AutoCloseable.
> When close() returned should the executor be in the shutdown phase or
> in the terminated phase?
>
> In other words should I implement close() like this (which I believe)
>
> public void close() {
> executor.shutdown();
> executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); //ignore
> any interrupted exceptions
> }
>
> or like this
>
> public void close() {
> executor.shutdown();
> }

That's really an arbitrary choice. What semantics do you want the 
close() to have?

Personally I don't think AutoCloseable is a good fit for an executor 
service as the lifetime of an autocloseable is tied to a block of code 
and that would be a rare usage for an executor service.

David Holmes

> - Kasper
>



More information about the core-libs-dev mailing list