Search in sources :

Example 46 with SynchronousQueue

use of java.util.concurrent.SynchronousQueue in project cdap by caskdata.

the class AbstractProgramController method addListener.

@Override
public final Cancellable addListener(Listener listener, final Executor listenerExecutor) {
    Preconditions.checkNotNull(listener, "Listener shouldn't be null.");
    Preconditions.checkNotNull(listenerExecutor, "Executor shouldn't be null.");
    final ListenerCaller caller = new ListenerCaller(listener, listenerExecutor);
    final Cancellable cancellable = new Cancellable() {

        @Override
        public void cancel() {
            // Simply remove the listener from the map through the executor and block on the completion
            Futures.getUnchecked(executor.submit(new Runnable() {

                @Override
                public void run() {
                    listeners.remove(caller);
                }
            }));
        }
    };
    try {
        // Use a synchronous queue to communicate the Cancellable to return
        final SynchronousQueue<Cancellable> result = new SynchronousQueue<>();
        // Use the single thread executor to add the listener and call init
        executor.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                Cancellable existing = listeners.get(caller);
                if (existing == null) {
                    listeners.put(caller, cancellable);
                    result.put(cancellable);
                    caller.init(getState(), getFailureCause());
                } else {
                    result.put(existing);
                }
                return null;
            }
        });
        return result.take();
    } catch (Exception e) {
        // there shouldn't be interrupted exception as well.
        throw Throwables.propagate(Throwables.getRootCause(e));
    }
}
Also used : Cancellable(org.apache.twill.common.Cancellable) SynchronousQueue(java.util.concurrent.SynchronousQueue)

Aggregations

SynchronousQueue (java.util.concurrent.SynchronousQueue)46 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)30 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 XMPPException (org.jivesoftware.smack.XMPPException)7 IOException (java.io.IOException)5 ExecutorService (java.util.concurrent.ExecutorService)5 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)5 XMPPConnection (org.jivesoftware.smack.XMPPConnection)5 ArrayList (java.util.ArrayList)4 BlockingQueue (java.util.concurrent.BlockingQueue)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 ThreadFactory (java.util.concurrent.ThreadFactory)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 AssignmentManager (org.apache.hadoop.hbase.master.AssignmentManager)4 ExecutorUtil (org.apache.solr.common.util.ExecutorUtil)4 DefaultSolrThreadFactory (org.apache.solr.util.DefaultSolrThreadFactory)4 BeforeClass (org.junit.BeforeClass)4 Test (org.junit.Test)4