Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed 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.gh.bmd.jrt.runner;
 
 
Utility class for creating and sharing runner instances.

Created by davide-maestroni on 9/9/14.

 
 public class Runners {
 
     private static final Object sMutex = new Object();
 
     private static final QueuedRunner sQueuedRunner = new QueuedRunner();
 
     private static final SequentialRunner sSequentialRunner = new SequentialRunner();
 
     private static Runner sSharedRunner;

    
Avoid direct instantiation.
 
     protected Runners() {
 
     }

    
Returns a runner employing a dynamic pool of threads.
The number of threads may increase when needed from the core to the maximum pool size. The number of threads exceeding the core size are kept alive when idle for the specified time. If they stay idle for a longer time they will be destroyed.

Parameters:
corePoolSize the number of threads to keep in the pool, even if they are idle.
maximumPoolSize the maximum number of threads to allow in the pool.
keepAliveTime when the number of threads is greater than the core one, this is the maximum time that excess idle threads will wait for new tasks before terminating.
keepAliveUnit the time unit for the keep alive time.
Returns:
the runner instance.
 
     @Nonnull
     public static Runner dynamicPoolRunner(final int corePoolSizefinal int maximumPoolSize,
             final long keepAliveTime, @Nonnull final TimeUnit keepAliveUnit) {
 
         return scheduledRunner(
                 new DynamicScheduledThreadExecutor(corePoolSizemaximumPoolSizekeepAliveTime,
                                                    keepAliveUnit));
     }

    
Returns a runner employing an optimum number of threads.

Returns:
the runner instance.
 
     @Nonnull
     public static Runner poolRunner() {
 
         return poolRunner(Runtime.getRuntime().availableProcessors() << 1);
     }

    
Returns a runner employing the specified number of threads.

Parameters:
poolSize the thread pool size.
Returns:
the runner instance.
Throws:
java.lang.IllegalArgumentException if the pool size is less than 1.
 
     @Nonnull
     public static Runner poolRunner(final int poolSize) {
 
         return scheduledRunner(Executors.newScheduledThreadPool(poolSize));
     }

    
Returns a runner providing ordering of executions based on priority.

Parameters:
wrapped the wrapped runner instance.
Returns:
the runner instance.
 
     @Nonnull
     public static PriorityRunner priorityRunner(@Nonnull final Runner wrapped) {
 
        return PriorityRunner.getInstance(wrapped);
    }

    
Returns the shared instance of a queued synchronous runner.

Returns:
the runner instance.
    @Nonnull
    public static Runner queuedRunner() {
        return ;
    }

    
Returns a runner employing the specified executor service.

Parameters:
service the executor service.
Returns:
the runner instance.
    @Nonnull
    public static Runner scheduledRunner(@Nonnull final ScheduledExecutorService service) {
        return new ScheduledRunner(service);
    }

    
Returns the shared instance of a sequential synchronous runner.

Returns:
the runner instance.
    @Nonnull
    public static Runner sequentialRunner() {
        return ;
    }

    
Returns the shared instance of a thread pool asynchronous runner.

Returns:
the runner instance.
    @Nonnull
    public static Runner sharedRunner() {
        synchronized () {
            if ( == null) {
                final int processors = Runtime.getRuntime().availableProcessors();
                 = dynamicPoolRunner((processors <= 2) ? processors : processors - 1,
                                                  (processors << 3) - 1, 3L, .);
            }
            return ;
        }
    }

    
Returns a runner throttling the number of running executions so to keep it under the specified limit.

Parameters:
wrapped the wrapped instance.
maxExecutions the maximum number of running executions.
Returns:
the runner instance.
Throws:
java.lang.IllegalArgumentException if the specified max number is less than 1.
    @Nonnull
    public static Runner throttlingRunner(@Nonnull final Runner wrappedfinal int maxExecutions) {
        return new ThrottlingRunner(wrappedmaxExecutions);
    }
New to GrepCode? Check out our FAQ X