Netty中的Future源码解读

来源:互联网 发布:游戏编程 知乎 编辑:程序博客网 时间:2024/05/16 23:38
/* * Copyright 2013 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * *   http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */package netty.test;import io.netty.util.concurrent.GenericFutureListener;import java.util.concurrent.CancellationException;import java.util.concurrent.TimeUnit;/** * 异步操作的结果类信息 */@SuppressWarnings("ClassNameSameAsAncestorName")public interface Future<V> extends java.util.concurrent.Future<V> {    /**     * 只有当I/O操作成功的被执行后,才会返回true     *     * @return     */    boolean isSuccess();    /**     * 只有当执行的操作,调用cancel方法后,才会返回true     *     * @return     */    boolean isCancellable();    /**     * 当I/O操作执行失败后,就会返回I/0操作执行失败的原因     * 如果成功后,就会返回null     * @return     */    Throwable cause();    /**     * 可以对每一个要获取结果添加一个明确的监听,当这个获取结果的future完成后,就会触发这个监听     * 当future已经完成后,监听会立即的执行     * 所谓的监听就是添加一些事件,比如成功后,说一下获奖感言,举个例子     * @param listener     * @return     */    Future<V> addListener(GenericFutureListener<? extends Future<? super V>> listener);    /**     * 类似上面的那个方法,只不过是添加多个监听     * @param listeners     * @return     */    Future<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners);    /**     * 这个是把监听给移除掉     * @param listener     * @return     */    Future<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener);    /**     * 这个是以集合的形式移除掉多个监听     * @param listeners     * @return     */    Future<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners);    /**     *同步的等待获取结果,否则会一直等待,直到任务结束拿到结果。     * 如果获取结果失败的话,会抛出失败的异常信息     * @return     * @throws InterruptedException     */    Future<V> sync() throws InterruptedException;    /**同步的等待获取结果,否则会一直等待,直到任务结束拿到结果。     * 如果获取结果失败的话,会抛出失败的异常信息     * 如果该线程一直处于阻塞的话,就中断该线程     *     * @return     */    Future<V> syncUninterruptibly();    /**     * 一直等待这个获取结果的任务完成,如果当前线程被中断的话,则会抛出异常     * @return     * @throws InterruptedException     */    Future<V> await() throws InterruptedException;    /**     * 阻塞获取结果,知道future完成为止,这个方法没有抛出异常,会默认的忽略中断的异常     * 详细看http://www.cnblogs.com/skywang12345/p/3479949.html     * @return     */    Future<V> awaitUninterruptibly();    /**     * 给等待获取结果加了一个时间限制,如果在确定的时间完成的话,返回true     * 如果当前的线程被中断的话,就会抛出异常信息     * @param timeout     * @param unit     * @return     * @throws InterruptedException     */    boolean await(long timeout, TimeUnit unit) throws InterruptedException;    /**     * 与上面的方法类似,只不过换了一个参数而已     * @param timeoutMillis     * @return     * @throws InterruptedException     */    boolean await(long timeoutMillis) throws InterruptedException;    /**     * 这个在那个吞掉中断的异常的基础上,加了时间的限制     * @param timeout     * @param unit     * @return     */    boolean awaitUninterruptibly(long timeout, TimeUnit unit);    boolean awaitUninterruptibly(long timeoutMillis);    /**     * 立马返回结果,如果future还没有完成,则返回null     * 不能用null来判断当前future是否完成,还需要去查看isDone函数,并且不在返回null     * @return     */    V getNow();    /**     * 用来cancellation是否成功     * @param mayInterruptIfRunning     * @return     */    @Override    boolean cancel(boolean mayInterruptIfRunning);}