简体   繁体   中英

Does Netty violate the contract of Future.cancel(…) method?

According the contract from method java.util.concurrent.Future#cancel :

After this method returns, subsequent calls to isDone will always return true.

Netty's Future interface extends it:

public interface Future<V> extends java.util.concurrent.Future<V>

So Netty should follow the contract. But in fact Netty does not. You can run this sample code:

import io.netty.util.concurrent.GlobalEventExecutor;
import io.netty.util.concurrent.Promise;

public class DefaultPromiseIsDoneTest {

    private final Promise<?> defaultPromise = GlobalEventExecutor.INSTANCE.newPromise();

    public static void main(String args[]) {
        DefaultPromiseIsDoneTest main = new DefaultPromiseIsDoneTest();
        main.isDoneTest();
    }

    private void isDoneTest() {
        defaultPromise.setUncancellable();
        defaultPromise.cancel(false);
        boolean isDone = defaultPromise.isDone();
        System.out.println(isDone);
    }
}

The console should print:

true

But in fact it print:

false

The following methods also violate the contract:

io.netty.channel.group.VoidChannelGroupFuture#isDone
io.netty.channel.VoidChannelPromise#isDone

I already created an issue on github: issue

But I still want to discuss this here in stackoverflow, because I think this is a pretty fundamental design decision for cancel & isDone methods of Future interface.

There are also some related topics:

Future cancel method documentation

Whether method cancel() in java.util.concurrent.Future shoud be blocking?

By the way, I am a fan of Netty :)

Netty confirmed this issue, please refer issue .

I think this contract is very possible be wrongly implemented in other Java async frameworks. Because it is really a counterintuitive contract when we first read it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM