Path: blob/master/src/java.base/share/classes/sun/nio/ch/PendingFuture.java
41159 views
/*1* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.nio.channels.*;28import java.util.concurrent.*;29import java.io.IOException;3031/**32* A Future for a pending I/O operation. A PendingFuture allows for the33* attachment of an additional arbitrary context object and a timer task.34*/3536final class PendingFuture<V,A> implements Future<V> {3738private final AsynchronousChannel channel;39private final CompletionHandler<V,? super A> handler;40private final A attachment;4142// true if result (or exception) is available43private volatile boolean haveResult;44private volatile V result;45private volatile Throwable exc;4647// latch for waiting (created lazily if needed)48private CountDownLatch latch;4950// optional timer task that is cancelled when result becomes available51private Future<?> timeoutTask;5253// optional context object54private volatile Object context;5556PendingFuture(AsynchronousChannel channel,57CompletionHandler<V,? super A> handler,58A attachment,59Object context)60{61this.channel = channel;62this.handler = handler;63this.attachment = attachment;64this.context = context;65}6667PendingFuture(AsynchronousChannel channel,68CompletionHandler<V,? super A> handler,69A attachment)70{71this.channel = channel;72this.handler = handler;73this.attachment = attachment;74}7576PendingFuture(AsynchronousChannel channel) {77this(channel, null, null);78}7980PendingFuture(AsynchronousChannel channel, Object context) {81this(channel, null, null, context);82}8384AsynchronousChannel channel() {85return channel;86}8788CompletionHandler<V,? super A> handler() {89return handler;90}9192A attachment() {93return attachment;94}9596void setContext(Object context) {97this.context = context;98}99100Object getContext() {101return context;102}103104void setTimeoutTask(Future<?> task) {105synchronized (this) {106if (haveResult) {107task.cancel(false);108} else {109this.timeoutTask = task;110}111}112}113114// creates latch if required; return true if caller needs to wait115private boolean prepareForWait() {116synchronized (this) {117if (haveResult) {118return false;119} else {120if (latch == null)121latch = new CountDownLatch(1);122return true;123}124}125}126127/**128* Sets the result, or a no-op if the result or exception is already set.129*/130void setResult(V res) {131synchronized (this) {132if (haveResult)133return;134result = res;135haveResult = true;136if (timeoutTask != null)137timeoutTask.cancel(false);138if (latch != null)139latch.countDown();140}141}142143/**144* Sets the result, or a no-op if the result or exception is already set.145*/146void setFailure(Throwable x) {147if (!(x instanceof IOException) && !(x instanceof SecurityException))148x = new IOException(x);149synchronized (this) {150if (haveResult)151return;152exc = x;153haveResult = true;154if (timeoutTask != null)155timeoutTask.cancel(false);156if (latch != null)157latch.countDown();158}159}160161/**162* Sets the result163*/164void setResult(V res, Throwable x) {165if (x == null) {166setResult(res);167} else {168setFailure(x);169}170}171172@Override173public V get() throws ExecutionException, InterruptedException {174if (!haveResult) {175boolean needToWait = prepareForWait();176if (needToWait)177latch.await();178}179if (exc != null) {180if (exc instanceof CancellationException)181throw new CancellationException();182throw new ExecutionException(exc);183}184return result;185}186187@Override188public V get(long timeout, TimeUnit unit)189throws ExecutionException, InterruptedException, TimeoutException190{191if (!haveResult) {192boolean needToWait = prepareForWait();193if (needToWait)194if (!latch.await(timeout, unit)) throw new TimeoutException();195}196if (exc != null) {197if (exc instanceof CancellationException)198throw new CancellationException();199throw new ExecutionException(exc);200}201return result;202}203204Throwable exception() {205return (exc instanceof CancellationException) ? null : exc;206}207208V value() {209return result;210}211212@Override213public boolean isCancelled() {214return (exc instanceof CancellationException);215}216217@Override218public boolean isDone() {219return haveResult;220}221222@Override223public boolean cancel(boolean mayInterruptIfRunning) {224synchronized (this) {225if (haveResult)226return false; // already completed227228// notify channel229if (channel() instanceof Cancellable)230((Cancellable)channel()).onCancel(this);231232// set result and cancel timer233exc = new CancellationException();234haveResult = true;235if (timeoutTask != null)236timeoutTask.cancel(false);237}238239// close channel if forceful cancel240if (mayInterruptIfRunning) {241try {242channel().close();243} catch (IOException ignore) { }244}245246// release waiters247if (latch != null)248latch.countDown();249return true;250}251}252253254