/** * Adds a listener to this future. If the future has not yet completed, the listener will be * notified of a response or exception in a runnable submitted to the ExecutorService provided. * If the future has completed, the listener will be notified immediately without forking to * a different thread. */ public void addListener(ActionListener<V> listener, ExecutorService executor, ThreadContext threadContext) { if (done) { // run the callback directly, we don't hold the lock and don't need to fork! notifyListener(listener, EsExecutors.newDirectExecutorService()); } else { final boolean run; // check done under lock since it could have been modified and protect modifications // to the list under lock synchronized (this) { if (done) { run = true; } else { listeners.add(new Tuple<>(ContextPreservingActionListener.wrapPreservingContext(listener, threadContext), executor)); run = false; } } if (run) { // run the callback directly, we don't hold the lock and don't need to fork! notifyListener(listener, EsExecutors.newDirectExecutorService()); } } }
/** * Adds a listener to this future. If the future has not yet completed, the listener will be * notified of a response or exception in a runnable submitted to the ExecutorService provided. * If the future has completed, the listener will be notified immediately without forking to * a different thread. */ public void addListener(ActionListener<V> listener, ExecutorService executor, ThreadContext threadContext) { if (done) { // run the callback directly, we don't hold the lock and don't need to fork! notifyListener(listener, EsExecutors.newDirectExecutorService()); } else { final boolean run; // check done under lock since it could have been modified and protect modifications // to the list under lock synchronized (this) { if (done) { run = true; } else { listeners.add(new Tuple<>(ContextPreservingActionListener.wrapPreservingContext(listener, threadContext), executor)); run = false; } } if (run) { // run the callback directly, we don't hold the lock and don't need to fork! notifyListener(listener, EsExecutors.newDirectExecutorService()); } } }
/** * Adds a listener to this future. If the future has not yet completed, the listener will be * notified of a response or exception in a runnable submitted to the ExecutorService provided. * If the future has completed, the listener will be notified immediately without forking to * a different thread. */ public void addListener(ActionListener<V> listener, ExecutorService executor, ThreadContext threadContext) { if (done) { // run the callback directly, we don't hold the lock and don't need to fork! notifyListener(listener, EsExecutors.newDirectExecutorService()); } else { final boolean run; // check done under lock since it could have been modified and protect modifications // to the list under lock synchronized (this) { if (done) { run = true; } else { listeners.add(new Tuple<>(ContextPreservingActionListener.wrapPreservingContext(listener, threadContext), executor)); run = false; } } if (run) { // run the callback directly, we don't hold the lock and don't need to fork! notifyListener(listener, EsExecutors.newDirectExecutorService()); } } }