java future 接口_Java中RunnableFuture接口的作用是什么?

package java.util.concurrent;

import java.util.concurrent.locks.*;

public class FutureTask implements RunnableFuture {

private final Sync sync;

public FutureTask(Callable callable) {

if (callable == null)

throw new NullPointerException();

sync = new Sync(callable);

}

public FutureTask(Runnable runnable, V result) {

sync = new Sync(Executors.callable(runnable, result));

}

public boolean isCancelled() {

return sync.innerIsCancelled();

}

public boolean isDone() {

return sync.innerIsDone();

}

public boolean cancel(boolean mayInterruptIfRunning) {

return sync.innerCancel(mayInterruptIfRunning);

}

/**

* @throws CancellationException {@inheritDoc}

*/

public V get() throws InterruptedException, ExecutionException {

return sync.innerGet();

}

public V get(long timeout, TimeUnit unit)

throws InterruptedException, ExecutionException, TimeoutException {

return sync.innerGet(unit.toNanos(timeout));

}

protected void done() { }

protected void set(V v) {

sync.innerSet(v);

}

protected void setException(Throwable t) {

sync.innerSetException(t);

}

public void run() {

sync.innerRun();

}

protected boolean runAndReset() {

return sync.innerRunAndReset();

}

private final class Sync extends AbstractQueuedSynchronizer {

private static final long serialVersionUID = -7828117401763700385L;

private static final int RUNNING = 1;

private static final int RAN = 2;

private static final int CANCELLED = 4;

private final Callable callable;

private V result;

private Throwable exception;

private volatile Thread runner;

Sync(Callable callable) {

this.callable = callable;

}

private boolean ranOrCancelled(int state) {

return (state & (RAN | CANCELLED)) != 0;

}

/**

* Implements AQS base acquire to succeed if ran or cancelled

*/

protected int tryAcquireShared(int ignore) {

return innerIsDone()? 1 : -1;

}

protected boolean tryReleaseShared(int ignore) {

runner = null;

return true;

}

boolean innerIsCancelled() {

return getState() == CANCELLED;

}

boolean innerIsDone() {

return ranOrCancelled(getState()) && runner == null;

}

V innerGet() throws InterruptedException, ExecutionException {

acquireSharedInterruptibly(0);

if (getState() == CANCELLED)

throw new CancellationException();

if (exception != null)

throw new ExecutionException(exception);

return result;

}

V innerGet(long nanosTimeout) throws InterruptedException, ExecutionException, TimeoutException {

if (!tryAcquireSharedNanos(0, nanosTimeout))

throw new TimeoutException();

if (getState() == CANCELLED)

throw new CancellationException();

if (exception != null)

throw new ExecutionException(exception);

return result;

}

void innerSet(V v) {

for (;;) {

int s = getState();

if (s == RAN)

return;

if (s == CANCELLED) {

// aggressively release to set runner to null,

// in case we are racing with a cancel request

// that will try to interrupt runner

releaseShared(0);

return;

}

if (compareAndSetState(s, RAN)) {

result = v;

releaseShared(0);

done();

return;

}

}

}

void innerSetException(Throwable t) {

for (;;) {

int s = getState();

if (s == RAN)

return;

if (s == CANCELLED) {

releaseShared(0);

return;

}

if (compareAndSetState(s, RAN)) {

exception = t;

result = null;

releaseShared(0);

done();

return;

}

}

}

boolean innerCancel(boolean mayInterruptIfRunning) {

for (;;) {

int s = getState();

if (ranOrCancelled(s))

return false;

if (compareAndSetState(s, CANCELLED))

break;

}

if (mayInterruptIfRunning) {

Thread r = runner;

if (r != null)

r.interrupt();

}

releaseShared(0);

done();

return true;

}

void innerRun() {

if (!compareAndSetState(0, RUNNING))

return;

try {

runner = Thread.currentThread();

if (getState() == RUNNING) // recheck after setting thread

innerSet(callable.call());

else

releaseShared(0); // cancel

} catch (Throwable ex) {

innerSetException(ex);

}

}

boolean innerRunAndReset() {

if (!compareAndSetState(0, RUNNING))

return false;

try {

runner = Thread.currentThread();

if (getState() == RUNNING)

callable.call(); // don't set result

runner = null;

return compareAndSetState(RUNNING, 0);

} catch (Throwable ex) {

innerSetException(ex);

return false;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值