/**
* The current number of processors that have been created.
*/
private int curProcessors = 0;
/**
* The minimum number of processors to start at initialization time.
*/
protected int minProcessors = 5;
/**
* The maximum number of processors allowed, or <0 for unlimited.
*/
private int maxProcessors = 20;
/**
* The set of processors that have been created but are not currently
* being used to process a request.
*/
private Stack<HttpProcessor> processors = new Stack<HttpProcessor>();
/**
* Get Processor from stack if there still exist, or create new one
* if current processor number didn't more than max number.
* Otherwise, null will be returned.
*
* @return instance of HttpProcessor
*/
protected HttpProcessor getProcessor() {
synchronized (processors) {
if (processors.size() > 0) {
return processors.pop();
}
if ((maxProcessors > 0) && (curProcessors < maxProcessors)) {
return (newProcessor());
} else {
if (maxProcessors < 0) {
return (newProcessor());
} else {
return (null);
}
}
}
}
private HttpProcessor newProcessor() {
HttpProcessor processor = new HttpProcessor(this);
processor.start();
curProcessors++;
return processor;
}
public void recycle(HttpProcessor processor) {
processors.push(processor);
}
/**
* The entry point of HttpProcessor. Assign an socket to the processor, it
* will start a new thread.
*
*
* @param socket
*/
public synchronized void assign(Socket socket) {
while(available) {
try {
wait();
} catch (InterruptedException e) {
// Do nothing.
}
}
this.socket = socket;
available = true;
notifyAll();
}