Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2013 Ning, Inc.
   *
   * Ning licenses this file to you under the Apache License, version 2.0
   * (the "License"); you may not use this file except in compliance with the
   * License.  You may obtain a copy of the License at:
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
  * License for the specific language governing permissions and limitations
  * under the License.
  */
 
 package com.ning.billing.commons.concurrent;
 
 import java.util.List;
Similar to Java's java.util.concurrent.Executors, but ensures either a LoggingExecutor or named FailsafeScheduledExecutor is used.
 
 @SuppressWarnings("UnusedDeclaration")
 public class Executors {
 
     /*
      * Fixed ThreadPool
      */
 
     public static ExecutorService newFixedThreadPool(final int nThreadsfinal String name) {
         return new LoggingExecutor(nThreadsnThreads, 0L, .new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory(name));
     }
 
     public static ExecutorService newFixedThreadPool(final int nThreadsfinal String namefinal long keepAliveTimefinal TimeUnit unit) {
         return new LoggingExecutor(nThreadsnThreadskeepAliveTimeunitnew NamedThreadFactory(name));
     }
 
     public static ExecutorService newFixedThreadPool(final int nThreadsfinal String namefinal long keepAliveTimefinal TimeUnit unitfinal BlockingQueue<RunnableworkQueue) {
         return new LoggingExecutor(nThreadsnThreadskeepAliveTimeunitworkQueuenew NamedThreadFactory(name));
     }
 
     public static ExecutorService newFixedThreadPool(final int nThreadsfinal long keepAliveTimefinal TimeUnit unitfinal BlockingQueue<RunnableworkQueuefinal ThreadFactory threadFactory) {
         return new LoggingExecutor(nThreadsnThreadskeepAliveTimeunitworkQueuethreadFactory);
     }
 
     public static ExecutorService newFixedThreadPool(final int nThreadsfinal String namefinal long keepAliveTimefinal TimeUnit unitfinal BlockingQueue<RunnableworkQueuefinal RejectedExecutionHandler handler) {
         return new LoggingExecutor(nThreadsnThreadskeepAliveTimeunitworkQueuenew NamedThreadFactory(name), handler);
     }
 
     public static ExecutorService newFixedThreadPool(final int nThreadsfinal long keepAliveTimefinal TimeUnit unitfinal BlockingQueue<RunnableworkQueuefinal ThreadFactory threadFactoryfinal RejectedExecutionHandler handler) {
         return new LoggingExecutor(nThreadsnThreadskeepAliveTimeunitworkQueuethreadFactoryhandler);
     }
 
     /*
      * Single threaded (fixed ThreadPool of 1)
      */
 
     public static ExecutorService newSingleThreadExecutor(final String name) {
         return new FinalizableDelegatedExecutorService(newFixedThreadPool(1, name));
     }
 
     public static ExecutorService newSingleThreadExecutor(final String namefinal long keepAliveTimefinal TimeUnit unit) {
         return new FinalizableDelegatedExecutorService(newFixedThreadPool(1, namekeepAliveTimeunit));
     }
 
     public static ExecutorService newSingleThreadExecutor(final String namefinal long keepAliveTimefinal TimeUnit unitfinal BlockingQueue<RunnableworkQueue) {
         return new FinalizableDelegatedExecutorService(newFixedThreadPool(1, namekeepAliveTimeunitworkQueue));
     }
 
     public static ExecutorService newSingleThreadExecutor(final long keepAliveTimefinal TimeUnit unitfinal BlockingQueue<RunnableworkQueuefinal ThreadFactory threadFactory) {
         return new FinalizableDelegatedExecutorService(newFixedThreadPool(1, keepAliveTimeunitworkQueuethreadFactory));
     }
 
     public static ExecutorService newSingleThreadExecutor(final String namefinal long keepAliveTimefinal TimeUnit unitfinal BlockingQueue<RunnableworkQueuefinal RejectedExecutionHandler handler) {
         return new FinalizableDelegatedExecutorService(newFixedThreadPool(1, namekeepAliveTimeunitworkQueuehandler));
     }
 
     public static ExecutorService newSingleThreadExecutor(final long keepAliveTimefinal TimeUnit unitfinal BlockingQueue<RunnableworkQueuefinal ThreadFactory threadFactoryfinal RejectedExecutionHandler handler) {
         return new FinalizableDelegatedExecutorService(newFixedThreadPool(1, keepAliveTimeunitworkQueuethreadFactoryhandler));
     }
 
     /*
      * Cached ThreadPool
     */
    public static ExecutorService newCachedThreadPool(final String name) {
        return new LoggingExecutor(0, ., 60L, .new SynchronousQueue<Runnable>(), new NamedThreadFactory(name));
    }
    public static ExecutorService newCachedThreadPool(final int minThreadsfinal int maxThreadsfinal String name) {
        return new LoggingExecutor(minThreadsmaxThreads, 60L, .new SynchronousQueue<Runnable>(), new NamedThreadFactory(name));
    }
    public static ExecutorService newCachedThreadPool(final int minThreadsfinal int maxThreadsfinal String namefinal long keepAliveTimefinal TimeUnit unit) {
        return new LoggingExecutor(minThreadsmaxThreadskeepAliveTimeunitnew SynchronousQueue<Runnable>(), new NamedThreadFactory(name));
    }
    public static ExecutorService newCachedThreadPool(final int minThreadsfinal int maxThreadsfinal String namefinal long keepAliveTimefinal TimeUnit unitfinal BlockingQueue<RunnableworkQueue) {
        return new LoggingExecutor(minThreadsmaxThreadskeepAliveTimeunitworkQueuenew NamedThreadFactory(name));
    }
    public static ExecutorService newCachedThreadPool(final int minThreadsfinal int maxThreadsfinal long keepAliveTimefinal TimeUnit unitfinal BlockingQueue<RunnableworkQueuefinal ThreadFactory threadFactory) {
        return new LoggingExecutor(minThreadsmaxThreadskeepAliveTimeunitworkQueuethreadFactory);
    }
    public static ExecutorService newCachedThreadPool(final int minThreadsfinal int maxThreadsfinal String namefinal long keepAliveTimefinal TimeUnit unitfinal RejectedExecutionHandler handler) {
        return new LoggingExecutor(minThreadsmaxThreadskeepAliveTimeunitnew SynchronousQueue<Runnable>(), new NamedThreadFactory(name), handler);
    }
    public static ExecutorService newCachedThreadPool(final int minThreadsfinal int maxThreadsfinal String namefinal long keepAliveTimefinal TimeUnit unitfinal BlockingQueue<RunnableworkQueuefinal RejectedExecutionHandler handler) {
        return new LoggingExecutor(minThreadsmaxThreadskeepAliveTimeunitworkQueuenew NamedThreadFactory(name), handler);
    }
    public static ExecutorService newCachedThreadPool(final int minThreadsfinal int maxThreadsfinal long keepAliveTimefinal TimeUnit unitfinal BlockingQueue<RunnableworkQueuefinal ThreadFactory threadFactoryfinal RejectedExecutionHandler handler) {
        return new LoggingExecutor(minThreadsmaxThreadskeepAliveTimeunitworkQueuethreadFactoryhandler);
    }
    /*
     * Single threaded scheduled executor
     */
    public static ScheduledExecutorService newSingleThreadScheduledExecutor(final String name) {
        return new DelegatedScheduledExecutorService(new FailsafeScheduledExecutor(name));
    }
    public static ScheduledExecutorService newSingleThreadScheduledExecutor(final ThreadFactory threadFactory) {
        return new DelegatedScheduledExecutorService(new FailsafeScheduledExecutor(1, threadFactory));
    }
    public static ScheduledExecutorService newSingleThreadScheduledExecutor(final String namefinal RejectedExecutionHandler handler) {
        return new DelegatedScheduledExecutorService(new FailsafeScheduledExecutor(1, namehandler));
    }
    public static ScheduledExecutorService newSingleThreadScheduledExecutor(final ThreadFactory threadFactoryfinal RejectedExecutionHandler handler) {
        return new DelegatedScheduledExecutorService(new FailsafeScheduledExecutor(1, threadFactoryhandler));
    }
    /*
     * Scheduled ThreadPool
     */
    public static ScheduledExecutorService newScheduledThreadPool(final int corePoolSizefinal String name) {
        return new FailsafeScheduledExecutor(corePoolSizename);
    }
    public static ScheduledExecutorService newScheduledThreadPool(final int corePoolSizefinal ThreadFactory threadFactory) {
        return new FailsafeScheduledExecutor(corePoolSizethreadFactory);
    }
    public static ScheduledExecutorService newScheduledThreadPool(final int corePoolSizefinal String namefinal RejectedExecutionHandler handler) {
        return new FailsafeScheduledExecutor(corePoolSizenamehandler);
    }
    public static ScheduledExecutorService newScheduledThreadPool(final int corePoolSizefinal ThreadFactory threadFactoryfinal RejectedExecutionHandler handler) {
        return new FailsafeScheduledExecutor(corePoolSizethreadFactoryhandler);
    }
    private static class DelegatedExecutorService extends AbstractExecutorService {
        private final ExecutorService e;
        DelegatedExecutorService(final ExecutorService executor) {
             = executor;
        }
        @Override
        public void execute(final Runnable command) {
            .execute(command);
        }
        @Override
        public void shutdown() {
            .shutdown();
        }
        @Override
        public List<RunnableshutdownNow() {
            return .shutdownNow();
        }
        @Override
        public boolean isShutdown() {
            return .isShutdown();
        }
        @Override
        public boolean isTerminated() {
            return .isTerminated();
        }
        @Override
        public boolean awaitTermination(final long timeoutfinal TimeUnit unitthrows InterruptedException {
            return .awaitTermination(timeoutunit);
        }
        @Override
        public Future<?> submit(final Runnable task) {
            return .submit(task);
        }
        @Override
        public <T> Future<T> submit(final Callable<T> task) {
            return .submit(task);
        }
        @Override
        public <T> Future<T> submit(final Runnable taskfinal T result) {
            return .submit(taskresult);
        }
        @Override
        public <T> List<Future<T>> invokeAll(final Collection<? extends Callable<T>> tasksthrows InterruptedException {
            return .invokeAll(tasks);
        }
        @Override
        public <T> List<Future<T>> invokeAll(final Collection<? extends Callable<T>> tasksfinal long timeoutfinal TimeUnit unitthrows InterruptedException {
            return .invokeAll(taskstimeoutunit);
        }
        @Override
        public <T> T invokeAny(final Collection<? extends Callable<T>> tasksthrows InterruptedExceptionExecutionException {
            return .invokeAny(tasks);
        }
        @Override
        public <T> T invokeAny(final Collection<? extends Callable<T>> tasksfinal long timeoutfinal TimeUnit unitthrows InterruptedExceptionExecutionExceptionTimeoutException {
            return .invokeAny(taskstimeoutunit);
        }
    }
    private static class FinalizableDelegatedExecutorService extends DelegatedExecutorService {
        FinalizableDelegatedExecutorService(final ExecutorService executor) {
            super(executor);
        }
        @Override
        protected void finalize() throws Throwable {
            super.shutdown();
            super.finalize();
        }
    }
    private static class DelegatedScheduledExecutorService extends DelegatedExecutorService implements ScheduledExecutorService {
        private final ScheduledExecutorService e;
            super(executor);
             = executor;
        }
        @Override
        public ScheduledFuture<?> schedule(final Runnable commandfinal long delayfinal TimeUnit unit) {
            return .schedule(commanddelayunit);
        }
        @Override
        public <V> ScheduledFuture<V> schedule(final Callable<V> callablefinal long delayfinal TimeUnit unit) {
            return .schedule(callabledelayunit);
        }
        @Override
        public ScheduledFuture<?> scheduleAtFixedRate(final Runnable commandfinal long initialDelayfinal long periodfinal TimeUnit unit) {
            return .scheduleAtFixedRate(commandinitialDelayperiodunit);
        }
        @Override
        public ScheduledFuture<?> scheduleWithFixedDelay(final Runnable commandfinal long initialDelayfinal long delayfinal TimeUnit unit) {
            return .scheduleWithFixedDelay(commandinitialDelaydelayunit);
        }
    }
    private Executors() {}
New to GrepCode? Check out our FAQ X