Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF 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 org.apache.catalina.tribes.util;
 
 
 public class ExecutorFactory {
 
     public static ExecutorService newThreadPool(int minThreadsint maxThreadslong maxIdleTimeTimeUnit unit) {
         TaskQueue taskqueue = new TaskQueue();
         ThreadPoolExecutor service = new TribesThreadPoolExecutor(minThreadsmaxThreadsmaxIdleTimeunit,taskqueue);
         taskqueue.setParent(service);
         return service;
     }
 
     public static ExecutorService newThreadPool(int minThreadsint maxThreadslong maxIdleTimeTimeUnit unitThreadFactory threadFactory) {
         TaskQueue taskqueue = new TaskQueue();
         ThreadPoolExecutor service = new TribesThreadPoolExecutor(minThreadsmaxThreadsmaxIdleTimeunit,taskqueuethreadFactory);
         taskqueue.setParent(service);
         return service;
     }
 
     // ---------------------------------------------- TribesThreadPoolExecutor Inner Class
     private static class TribesThreadPoolExecutor extends ThreadPoolExecutor {
         public TribesThreadPoolExecutor(int corePoolSizeint maximumPoolSizelong keepAliveTimeTimeUnit unitBlockingQueue<RunnableworkQueueRejectedExecutionHandler handler) {
             super(corePoolSizemaximumPoolSizekeepAliveTimeunitworkQueuehandler);
         }
 
         public TribesThreadPoolExecutor(int corePoolSizeint maximumPoolSizelong keepAliveTimeTimeUnit unitBlockingQueue<RunnableworkQueueThreadFactory threadFactory,
                 RejectedExecutionHandler handler) {
             super(corePoolSizemaximumPoolSizekeepAliveTimeunitworkQueuethreadFactoryhandler);
         }
 
         public TribesThreadPoolExecutor(int corePoolSizeint maximumPoolSizelong keepAliveTimeTimeUnit unitBlockingQueue<RunnableworkQueueThreadFactory threadFactory) {
             super(corePoolSizemaximumPoolSizekeepAliveTimeunitworkQueuethreadFactory);
         }
 
         public TribesThreadPoolExecutor(int corePoolSizeint maximumPoolSizelong keepAliveTimeTimeUnit unitBlockingQueue<RunnableworkQueue) {
             super(corePoolSizemaximumPoolSizekeepAliveTimeunitworkQueue);
         }
 
         @Override
         public void execute(Runnable command) {
             try {
                 super.execute(command);
             } catch (RejectedExecutionException rx) {
                 if (super.getQueue() instanceof TaskQueue) {
                     TaskQueue queue = (TaskQueue)super.getQueue();
                     if (!queue.force(command)) {
                         throw new RejectedExecutionException("Queue capacity is full.");
                     }
                 }
             }
         }
     }
 
      // ---------------------------------------------- TaskQueue Inner Class
     private static class TaskQueue extends LinkedBlockingQueue<Runnable> {
         private static final long serialVersionUID = 1L;
 
         ThreadPoolExecutor parent = null;
 
         public TaskQueue() {
             super();
         }
 
         public void setParent(ThreadPoolExecutor tp) {
              = tp;
         }
 
         public boolean force(Runnable o) {
             if ( .isShutdown() ) throw new RejectedExecutionException("Executor not running, can't force a command into the queue");
             return super.offer(o); //forces the item onto the queue, to be used if the task is rejected
         }
 
         @Override
         public boolean offer(Runnable o) {
            //we can't do any checks
            if (==nullreturn super.offer(o);
            //we are maxed out on threads, simply queue the object
            if (.getPoolSize() == .getMaximumPoolSize()) return super.offer(o);
            //we have idle threads, just add it to the queue
            //this is an approximation, so it could use some tuning
            if (.getActiveCount()<(.getPoolSize())) return super.offer(o);
            //if we have less threads than maximum force creation of a new thread
            if (.getPoolSize()<.getMaximumPoolSize()) return false;
            //if we reached here, we need to add it to the queue
            return super.offer(o);
        }
    }
New to GrepCode? Check out our FAQ X