/*
 * 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.tomcat.util.threads;

import java.util.Collection;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
 * As task queue specifically designed to run with a thread pool executor.
 * The task queue is optimised to properly utilize threads within 
 * a thread pool executor. If you use a normal queue, the executor will spawn threads
 * when there are idle threads and you wont be able to force items unto the queue itself 
 * @author fhanik
 *
 */
public class TaskQueue extends LinkedBlockingQueue<Runnable> {
    private ThreadPoolExecutor parent = null;

    public TaskQueue() {
        super();
    }

    public TaskQueue(int capacity) {
        super(capacity);
    }

    public TaskQueue(Collection<? extends Runnable> c) {
        super(c);
    }

    public void setParent(ThreadPoolExecutor tp) {
        parent = tp;
    }
    
    public boolean force(Runnable o) {
        if ( parent.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
    }

    public boolean force(Runnable o, long timeout, TimeUnit unit) throws InterruptedException {
        if ( parent.isShutdown() ) throw new RejectedExecutionException("Executor not running, can't force a command into the queue");
        return super.offer(o,timeout,unit); //forces the item onto the queue, to be used if the task is rejected
    }

    public boolean offer(Runnable o) {
        if (parent != null && parent.getPoolSize()<parent.getMaximumPoolSize()){
        	return false;
        } else {
            //if we reached here, we need to add it to the queue
            //or can't do any checks
            return super.offer(o);
        }

    }
}

@@ -2467,31 +2467,62 @@
 nable o) %7B%0A 
+     //we can't do any checks%0A
        
+ 
 if (parent !
@@ -2523,20 +2523,114 @@
 rent
- != null && 
+==null) return super.offer(o);%0A        //we are maxed out on threads, simply queue the object%0A        if (
 pare
@@ -2645,17 +2645,20 @@
 olSize()
-%3C
+ == 
 parent.g
@@ -2681,54 +2681,326 @@
 e())
-%7B%0A        %09return false;%0A        %7D else %7B%0A    
+ return super.offer(o);%0A        //we have idle threads, just add it to the queue%0A        if (parent.getActiveCount()%3C(parent.getPoolSize())) return super.offer(o);%0A        //if we have less threads than maximum force creation of a new thread%0A        if (parent.getPoolSize()%3Cparent.getMaximumPoolSize()) return false;%0A
     
@@ -3060,49 +3060,8 @@
 eue%0A
-            //or can't do any checks%0A    
     
@@ -3091,19 +3091,8 @@
 o);%0A
-        %7D%0A%0A
     

Revert http://svn.apache.org/viewvc?view=rev&revision=763566 The code is not the same as it was before. The patch applied, while it looks the same will grow the pool the max threads even though it doesn't need to