/** * Returns {@code true} if and only if the I/O operation was completed * successfully. */ booleanisSuccess();
/** * returns {@code true} if and only if the operation can be cancelled via {@link #cancel(boolean)}. */ booleanisCancellable();
/** * Returns the cause of the failed I/O operation if the I/O operation has * failed. * * @return the cause of the failure. * {@code null} if succeeded or this future is not * completed yet. */ Throwable cause();
/** * Adds the specified listener to this future. The * specified listener is notified when this future is * {@linkplain #isDone() done}. If this future is already * completed, the specified listener is notified immediately. */ Future<V> addListener(GenericFutureListener<? extends Future<? super V>> listener);
/** * Adds the specified listeners to this future. The * specified listeners are notified when this future is * {@linkplain #isDone() done}. If this future is already * completed, the specified listeners are notified immediately. */ Future<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners);
/** * Removes the first occurrence of the specified listener from this future. * The specified listener is no longer notified when this * future is {@linkplain #isDone() done}. If the specified * listener is not associated with this future, this method * does nothing and returns silently. */ Future<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener);
/** * Removes the first occurrence for each of the listeners from this future. * The specified listeners are no longer notified when this * future is {@linkplain #isDone() done}. If the specified * listeners are not associated with this future, this method * does nothing and returns silently. */ Future<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners);
/** * Waits for this future until it is done, and rethrows the cause of the failure if this future * failed. */ Future<V> sync()throws InterruptedException;
/** * Waits for this future until it is done, and rethrows the cause of the failure if this future * failed. */ Future<V> syncUninterruptibly();
/** * Waits for this future to be completed. * * @throws InterruptedException * if the current thread was interrupted */ Future<V> await()throws InterruptedException;
/** * Waits for this future to be completed without * interruption. This method catches an {@link InterruptedException} and * discards it silently. */ Future<V> awaitUninterruptibly();
/** * Waits for this future to be completed within the * specified time limit. * * @return {@code true} if and only if the future was completed within * the specified time limit * * @throws InterruptedException * if the current thread was interrupted */ booleanawait(long timeout, TimeUnit unit)throws InterruptedException;
/** * Waits for this future to be completed within the * specified time limit. * * @return {@code true} if and only if the future was completed within * the specified time limit * * @throws InterruptedException * if the current thread was interrupted */ booleanawait(long timeoutMillis)throws InterruptedException;
/** * Waits for this future to be completed within the * specified time limit without interruption. This method catches an * {@link InterruptedException} and discards it silently. * * @return {@code true} if and only if the future was completed within * the specified time limit */ booleanawaitUninterruptibly(long timeout, TimeUnit unit);
/** * Waits for this future to be completed within the * specified time limit without interruption. This method catches an * {@link InterruptedException} and discards it silently. * * @return {@code true} if and only if the future was completed within * the specified time limit */ booleanawaitUninterruptibly(long timeoutMillis);
/** * Return the result without blocking. If the future is not done yet this will return {@code null}. * * As it is possible that a {@code null} value is used to mark the future as successful you also need to check * if the future is really done with {@link #isDone()} and not relay on the returned {@code null} value. */ V getNow();
/** * {@inheritDoc} * * If the cancellation was successful it will fail the future with an {@link CancellationException}. */ @Override booleancancel(boolean mayInterruptIfRunning); }
publicinterfaceChannelFutureextendsFuture<Void> {
/** * Returns a channel where the I/O operation associated with this * future takes place. */ Channel channel();
@Override ChannelFuture addListener(GenericFutureListener<? extends Future<? super Void>> listener);
@Override ChannelFuture addListeners(GenericFutureListener<? extends Future<? super Void>>... listeners);
@Override ChannelFuture removeListener(GenericFutureListener<? extends Future<? super Void>> listener);
@Override ChannelFuture removeListeners(GenericFutureListener<? extends Future<? super Void>>... listeners);
/** * Returns {@code true} if this {@link ChannelFuture} is a void future and so not allow to call any of the * following methods: * <ul> * <li>{@link #addListener(GenericFutureListener)}</li> * <li>{@link #addListeners(GenericFutureListener[])}</li> * <li>{@link #await()}</li> * <li>{@link #await(long, TimeUnit)} ()}</li> * <li>{@link #await(long)} ()}</li> * <li>{@link #awaitUninterruptibly()}</li> * <li>{@link #sync()}</li> * <li>{@link #syncUninterruptibly()}</li> * </ul> */ booleanisVoid();
private ChannelFuture doBind(final SocketAddress localAddress) { //启动过程先拿到initAndRegister的Future finalChannelFutureregFuture= initAndRegister(); finalChannelchannel= regFuture.channel(); if (regFuture.cause() != null) { return regFuture; } //如果这个时候已经注册完毕了,那就直接调用 if (regFuture.isDone()) { // At this point we know that the registration was complete and successful. // 创建一个新的promise promise和future的差别就是promise是可写可修改状态的 ChannelPromisepromise= channel.newPromise(); //doBind的是一个异步操作,下面直接就返回了这个promise了 //等doBind操作完后修改promise的状态 doBind0(regFuture, channel, localAddress, promise); return promise; } else { // Registration future is almost always fulfilled already, but just in case it's not. //这里也好理解,因为之前判断注册任务没有完成,所以这里就挂载一个监听器,如果完成了的话就开始doBind的操作,从而实现整个过程都是异步的 finalPendingRegistrationPromisepromise=newPendingRegistrationPromise(channel); regFuture.addListener(newChannelFutureListener() { @Override publicvoidoperationComplete(ChannelFuture future)throws Exception { Throwablecause= future.cause(); if (cause != null) { // Registration on the EventLoop failed so fail the ChannelPromise directly to not cause an // IllegalStateException once we try to access the EventLoop of the Channel. promise.setFailure(cause); } else { // Registration was successful, so set the correct executor to use. // See https://github.com/netty/netty/issues/2586 promise.registered();