@Getter @Setter @Accessors(chain = true) public class LombokBean { private String field; private PropertyChangeSupport support = new PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener listener) { support.addPropertyChangeListener(listener); }
public void removePropertyChangeListener(PropertyChangeListener listener) { support.removePropertyChangeListener(listener); }
@Slf4j public class FutureTask<V> implements RunnableFuture<V> { public static final int STOP_STAMP = 5; private final Sync sync; public FutureTask(Callable<V> callable) { if (callable == null) throw new NullPointerException(); sync = new Sync(callable); }
@Override public boolean isCancelled() { return sync.innerIsCancelled(); }
@Override public boolean isDone() { return sync.innerIsDone(); }
@Override public boolean cancel(boolean mayInterruptIfRunning) { return sync.innerCancel(mayInterruptIfRunning); }
/** * @throws CancellationException {@inheritDoc} * 每个任务都有可能抛出异常 */ @Override public V get() throws ExecutionException, InterruptedException {
return sync.innerGet(); }
/** * @throws CancellationException {@inheritDoc} * 最多延迟 xx 获取结果 */ @Override public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return sync.innerGet(unit.toNanos(timeout)); }
/** * Sets the result of this Future to the given value unless this future * has already been set or has been cancelled. This method is invoked * internally by the <tt>run</tt> method upon successful completion of * the computation. * * @param v the value */ protected void set(V v) { sync.innerSet(v); }
/** * @param t the cause of failure */ protected void setException(Throwable t) { sync.innerSetException(t); }
@Override public void run() { sync.innerRun(); }
/** * Synchronization control for FutureTask. Note that this must be a * non-static inner class in order to invoke the protected <tt>done</tt> * method. For clarity, all inner class support methods are same as * outer, prefixed with "inner". * <p> * Uses AQS sync state to represent run status */ private final class Sync extends AbstractQueuedSynchronizer { private static final long serialVersionUID = -7828117401763700385L;
/** * State value representing that task is ready to run */ private static final int READY = 0; /** * State value representing that task is running */ private static final int RUNNING = 1; /** * State value representing that task ran */ private static final int RAN = 2; /** * State value representing that task was cancelled */ private static final int CANCELLED = 4;
/** * The underlying callable */ private final Callable<V> callable; /** * The result to return from get() */ private V result; /** * The exception to throw from get() */ private Throwable exception;
/** * The thread running task. When nulled after set/cancel, this * indicates that the results are accessible. Must be volatile, to * ensure visibility upon completion. */ private volatile Thread runner;
/** * Implements AQS base acquire to succeed if ran or cancelled */ @Override protected int tryAcquireShared(int ignore) { return innerIsDone() ? 1 : -1; }
/** * Implements AQS base release to always signal after setting final * done status by nulling runner thread. */ @Override protected boolean tryReleaseShared(int ignore) { runner = null; return true; }
/** * Inner is cancelled boolean. * * @return the boolean */ boolean innerIsCancelled() { return getState() == CANCELLED; }
/** * Inner get v. * * @return the v * @throws InterruptedException the interrupted exception * @throws ExecutionException the execution exception */ V innerGet() throws InterruptedException, ExecutionException { acquireSharedInterruptibly(0); if (getState() == CANCELLED) throw new CancellationException(); if (exception != null) throw new ExecutionException(exception); return result; }
/** * Inner get v. * * @param nanosTimeout the nanos timeout * @return the v * @throws InterruptedException the interrupted exception * @throws ExecutionException the execution exception * @throws TimeoutException the timeout exception */ 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; }
/** * Inner set. * * @param v the v */ 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); return; } } }
/** * 设置异常 * * @param t the t */ void innerSetException(Throwable t) { 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)) { exception = t; releaseShared(0); return; } } }
/** * 避免死循环 * * @param mayStopIfRunning the may stop if running * @return boolean boolean */ boolean innerStop(boolean mayStopIfRunning) { for (; ; ) { int s = getState(); if (ranOrCancelled(s)) return false; if (compareAndSetState(s, CANCELLED)) break; } if (mayStopIfRunning) { Thread r = runner; if (r != null) { r.stop();//这里调用线程stop方法 setState(STOP_STAMP); } } releaseShared(0);
return true; }
/** * 设置关闭 * * @param mayInterruptIfRunning the may interrupt if running * @return the boolean */ 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); return true; }