首页

关于grizzly-nio-framework源码包中多线程并发java.util.concurrent.Future的实现类com.sun.grizzly.util.FutureImpl源码说明

标签:grizzly-nio-framework,Future,FutureImpl,多线程,并发,放开值现场     发布时间:2018-05-16   

一、前言

关于grizzly-nio-framework-1.9.15b源码包中com.sun.grizzly.util.FutureImpl实现了多线程并发java.util.concurrent.Future接口,详情参考代码示例。

二、代码示例

package com.sun.grizzly.util;@b@@b@import java.util.concurrent.CancellationException;@b@import java.util.concurrent.ExecutionException;@b@import java.util.concurrent.Future;@b@import java.util.concurrent.TimeUnit;@b@import java.util.concurrent.TimeoutException;@b@@b@public class FutureImpl<R>@b@  implements Future<R>@b@{@b@  private final Object sync;@b@  private boolean isDone;@b@  private boolean isCancelled;@b@  private Throwable failure;@b@  protected R result;@b@@b@  public FutureImpl()@b@  {@b@    this(new Object());@b@  }@b@@b@  public FutureImpl(Object sync) {@b@    this.sync = sync;@b@  }@b@@b@  public R getResult() {@b@    synchronized (this.sync) {@b@      return this.result;@b@    }@b@  }@b@@b@  public void setResult(R result) {@b@    synchronized (this.sync) {@b@      this.result = result;@b@      notifyHaveResult();@b@    }@b@  }@b@@b@  public boolean cancel(boolean mayInterruptIfRunning) {@b@    synchronized (this.sync) {@b@      this.isCancelled = true;@b@      notifyHaveResult();@b@      return true;@b@    }@b@  }@b@@b@  public boolean isCancelled() {@b@    synchronized (this.sync) {@b@      return this.isCancelled;@b@    }@b@  }@b@@b@  public boolean isDone() {@b@    synchronized (this.sync) {@b@      return this.isDone;@b@    }@b@  }@b@@b@  public R get() throws InterruptedException, ExecutionException {@b@    synchronized (this.sync)@b@    {@b@      if (!(this.isDone)) break label62;@b@      if (this.isCancelled)@b@        throw new CancellationException();@b@      if (this.failure != null)@b@        throw new ExecutionException(this.failure);@b@      if (this.result == null) break label62;@b@      return this.result;@b@@b@      label62: this.sync.wait();@b@    }@b@  }@b@@b@  public R get(long timeout, TimeUnit unit)@b@    throws InterruptedException, ExecutionException, TimeoutException@b@  {@b@    long startTime = System.currentTimeMillis();@b@    long timeoutMillis = TimeUnit.MILLISECONDS.convert(timeout, unit);@b@    synchronized (this.sync)@b@    {@b@      if (!(this.isDone)) break label79;@b@      if (this.isCancelled)@b@        throw new CancellationException();@b@      if (this.failure != null)@b@        throw new ExecutionException(this.failure);@b@      if (this.result == null) break label99;@b@      return this.result;@b@@b@      label79: if (System.currentTimeMillis() - startTime <= timeoutMillis) break label99;@b@      throw new TimeoutException();@b@@b@      label99: this.sync.wait(timeoutMillis);@b@    }@b@  }@b@@b@  public void setException(Throwable failure)@b@  {@b@    synchronized (this.sync) {@b@      this.failure = failure;@b@      notifyHaveResult();@b@    }@b@  }@b@@b@  protected void notifyHaveResult() {@b@    this.isDone = true;@b@    this.sync.notifyAll();@b@  }@b@}