package org.apache.coyote; import java.util.ArrayDeque; import java.util.Deque; import org.apache.coyote.AbstractProtocol.ConnectionHandler; import org.apache.tomcat.util.collections.SynchronizedStack; protected static class RecycledProcessors { private final transient ConnectionHandler handler; private final Deque queue = new ArrayDeque<>(SynchronizedStack.DEFAULT_SIZE); public RecycledProcessors(ConnectionHandler handler) { this.handler = handler; } @SuppressWarnings("sync-override") // Size may exceed cache size a bit public boolean push(Processor processor) { synchronized (queue) { if ( queue.contains(processor) ) { return true; } } int cacheSize = handler.getProtocol().getProcessorCache(); boolean offer = true; if ( cacheSize != -1 ) { synchronized (queue) { offer = queue.size() < cacheSize; } } //avoid over growing our cache or add after we have stopped boolean result = false; if (offer) { synchronized (queue) { result = queue.offerLast(processor); } } if (!result) handler.unregister(processor); return result; } @SuppressWarnings("sync-override") // OK if size is too big briefly public Processor pop() { synchronized (queue) { return queue.pollLast(); } } public void clear() { synchronized (queue) { Processor next = pop(); while (next != null) { handler.unregister(next); next = pop(); } } } }