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;
 
 
Scheduled thread pool executor implementing a dynamic allocation of threads.
When the numbers of running threads reaches the maximum pool size, further commands are queued for later execution.

Created by davide-maestroni on 1/23/15.

 
 
     private final ThreadPoolExecutor mExecutor;

    
Constructor.

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, 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.
 
     DynamicScheduledThreadExecutor(final int corePoolSizefinal int maximumPoolSize,
             final long keepAliveTime, @Nonnull final TimeUnit keepAliveUnit) {
 
         super(1);
         final RejectingBlockingQueue internalQueue = new RejectingBlockingQueue();
         final QueueRejectedExecutionHandler rejectedExecutionHandler =
                 new QueueRejectedExecutionHandler(internalQueue);
          =
                 new ThreadPoolExecutor(corePoolSizemaximumPoolSizekeepAliveTimekeepAliveUnit,
                                        internalQueuerejectedExecutionHandler);
     }
 
     @Nonnull
     @Override
     public ScheduledFuture<?> schedule(final Runnable commandfinal long delay,
             final TimeUnit unit) {
 
         return super.schedule(new CommandRunnable(mExecutor, command), delayunit);
     }

    
Runnable executing another runnable.
 
     private static class CommandRunnable implements Runnable {
 
         private final Runnable mCommand;
 
         private final ThreadPoolExecutor mExecutor;

        
Constructor.

Parameters:
executor the executor instance.
command the command to execute.
 
         private CommandRunnable(@Nonnull final ThreadPoolExecutor executor,
                 @Nonnull final Runnable command) {
 
              = executor;
              = command;
         }
 
         public void run() {
 
             .execute();
         }
     }

    
Handler of rejected execution queueing the rejected command.
 
     private static class QueueRejectedExecutionHandler implements RejectedExecutionHandler {
 
        private final RejectingBlockingQueue mQueue;

        
Constructor.

Parameters:
queue the command queue.
        private QueueRejectedExecutionHandler(@Nonnull final RejectingBlockingQueue queue) {
             = queue;
        }
        public void rejectedExecution(final Runnable runnable,
                final ThreadPoolExecutor threadPoolExecutor) {
            .push(runnable);
        }
    }

    
Implementation of a blocking queue rejecting the addition of any new element.
    private static class RejectingBlockingQueue extends LinkedBlockingQueue<Runnable> {
        // just don't care...
        private static final long serialVersionUID = -1;

        
Constructor.
        private RejectingBlockingQueue() {
            super(.);
        }
        @Override
        public boolean add(final Runnable runnable) {
            return false;
        }
        @Override
        public boolean addAll(final Collection<? extends Runnablec) {
            return false;
        }
        @Override
        public int remainingCapacity() {
            return 0;
        }
        @Override
        public void put(final Runnable runnablethrows InterruptedException {
            throw new InterruptedException();
        }
        @Override
        public boolean offer(final Runnable runnablefinal long timeout,
                @Nonnull final TimeUnit timeUnitthrows InterruptedException {
            return false;
        }
        @Override
        public boolean offer(@Nonnull final Runnable runnable) {
            return false;
        }
        private boolean push(@Nonnull final Runnable runnable) {
            return super.offer(runnable);
        }
    }
New to GrepCode? Check out our FAQ X