Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 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.mogwee.executors;
 
 import java.util.List;
Similar to Java's java.util.concurrent.Executors, but ensures either a LoggingExecutor or named FailsafeScheduledExecutor is used.
 
 public class Executors
 {
     public static ExecutorService newFixedThreadPool(int nThreadsString name)
     {
         return new LoggingExecutor(nThreadsnThreads, 0L, .new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory(name));
     }
 
     public static ExecutorService newSingleThreadExecutor(String name)
     {
         return new FinalizableDelegatedExecutorService(new LoggingExecutor(1, 1, 0L, .new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory(name)));
     }
 
     public static ExecutorService newCachedThreadPool(String name)
     {
         return new LoggingExecutor(0, ., 60L, .new SynchronousQueue<Runnable>(), new NamedThreadFactory(name));
     }
 
     {
         return new DelegatedScheduledExecutorService(new FailsafeScheduledExecutor(name));
     }
 
     public static ScheduledExecutorService newScheduledThreadPool(int corePoolSizeString name)
     {
         return new FailsafeScheduledExecutor(corePoolSizename);
     }
 
     private static class DelegatedExecutorService extends AbstractExecutorService
     {
         private final ExecutorService e;
 
         DelegatedExecutorService(ExecutorService executor)
         {
              = executor;
         }
 
         @Override
         public void execute(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(long timeoutTimeUnit unitthrows InterruptedException
        {
            return .awaitTermination(timeoutunit);
        }
        @Override
        public Future<?> submit(Runnable task)
        {
            return .submit(task);
        }
        @Override
        public <T> Future<T> submit(Callable<T> task)
        {
            return .submit(task);
        }
        @Override
        public <T> Future<T> submit(Runnable task, T result)
        {
            return .submit(taskresult);
        }
        @Override
        public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasksthrows InterruptedException
        {
            return .invokeAll(tasks);
        }
        @Override
        public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> taskslong timeoutTimeUnit unitthrows InterruptedException
        {
            return .invokeAll(taskstimeoutunit);
        }
        @Override
        public <T> T invokeAny(Collection<? extends Callable<T>> tasksthrows InterruptedExceptionExecutionException
        {
            return .invokeAny(tasks);
        }
        @Override
        public <T> T invokeAny(Collection<? extends Callable<T>> taskslong timeoutTimeUnit unitthrows InterruptedExceptionExecutionExceptionTimeoutException
        {
            return .invokeAny(taskstimeoutunit);
        }
    }
    private static class FinalizableDelegatedExecutorService extends DelegatedExecutorService
    {
        {
            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(Runnable commandlong delayTimeUnit unit)
        {
            return .schedule(commanddelayunit);
        }
        @Override
        public <V> ScheduledFuture<V> schedule(Callable<V> callablelong delayTimeUnit unit)
        {
            return .schedule(callabledelayunit);
        }
        @Override
        public ScheduledFuture<?> scheduleAtFixedRate(Runnable commandlong initialDelaylong periodTimeUnit unit)
        {
            return .scheduleAtFixedRate(commandinitialDelayperiodunit);
        }
        @Override
        public ScheduledFuture<?> scheduleWithFixedDelay(Runnable commandlong initialDelaylong delayTimeUnit unit)
        {
            return .scheduleWithFixedDelay(commandinitialDelaydelayunit);
        }
    }
    private Executors()
    {
    }
New to GrepCode? Check out our FAQ X