Netty是否违反java.util.concurrent.Future.cancel()方法的契约?
java.util.concurrent.Future#cancel Contract Hey folks, I've run into a confusing issue with Netty's Future implementation that I wanted to discuss with the community.
Netty's Future interface extends the standard JDK java.util.concurrent.Future, so it should strictly follow all the contracts defined by that parent interface. But here's the problem: when I call cancel() on a Netty DefaultPromise, the subsequent isDone() call doesn't behave as the JDK contract specifies.
The official contract for java.util.concurrent.Future#cancel clearly states:
该方法返回后,后续调用isDone将始终返回true。
But when I run this simple test code:
import io.netty.util.concurrent.DefaultPromise; import io.netty.util.concurrent.EventExecutor; import io.netty.util.concurrent.SingleThreadEventExecutor; import io.netty.util.concurrent.ThreadPerTaskExecutor; public class NettyFutureCancelTest { public static void main(String[] args) { EventExecutor executor = new SingleThreadEventExecutor(new ThreadPerTaskExecutor(r -> new Thread(r)), false); DefaultPromise<Void> promise = new DefaultPromise<>(executor); promise.cancel(false); System.out.println(promise.isDone()); // Expected output: true, Actual output: false } }
The result is false instead of the expected true—a clear violation of the contract.
What's more, this isn't an isolated case: I've found other methods in Netty's Future implementation that also don't align with the JDK's contract requirements.
I've already created an issue on GitHub to report this, but I wanted to bring it up here too because:
- I want to double-check that I'm not misinterpreting the JDK's contract rules
- I'm curious if other developers have encountered similar behavior in their projects
- I'd love to hear if there are any workarounds or official explanations for this discrepancy
内容的提问来源于stack exchange,提问作者Jason




