FileDocCategorySizeDatePackage
MasterSlaveWorkerThread.javaAPI DocApache Tomcat 6.0.144397Fri Jul 20 04:20:34 BST 2007org.apache.tomcat.util.net

MasterSlaveWorkerThread

public class MasterSlaveWorkerThread extends Object implements Runnable
Regular master slave thread pool. Slave threads will wait for work.

Fields Summary
protected PoolTcpEndpoint
endpoint
protected String
threadName
protected boolean
stopped
private Object
threadSync
private Thread
thread
private boolean
available
private Socket
socket
private TcpConnection
con
private Object[]
threadData
Constructors Summary
public MasterSlaveWorkerThread(PoolTcpEndpoint endpoint, String threadName)


    
         
        this.endpoint = endpoint;
        this.threadName = threadName;
    
Methods Summary
synchronized voidassign(java.net.Socket socket)
Process an incoming TCP/IP connection on the specified socket. Any exception that occurs during processing must be logged and swallowed. NOTE: This method is called from our Connector's thread. We must assign it to our own thread so that multiple simultaneous requests can be handled.

param
socket TCP socket to process


        // Wait for the Processor to get the previous Socket
        while (available) {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }

        // Store the newly available Socket and notify our thread
        this.socket = socket;
        available = true;
        notifyAll();

    
private synchronized java.net.Socketawait()
Await a newly assigned Socket from our Connector, or null if we are supposed to shut down.


        // Wait for the Connector to provide a new Socket
        while (!available) {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }

        // Notify the Connector that we have received this Socket
        Socket socket = this.socket;
        available = false;
        notifyAll();

        return (socket);

    
public voidrun()
The background thread that listens for incoming TCP/IP connections and hands them off to an appropriate processor.


        // Process requests until we receive a shutdown signal
        while (!stopped) {

            // Wait for the next socket to be assigned
            Socket socket = await();
            if (socket == null)
                continue;

            // Process the request from this socket
            endpoint.processSocket(socket, con, threadData);

            // Finish up this request
            endpoint.recycleWorkerThread(this);

        }

        // Tell threadStop() we have shut ourselves down successfully
        synchronized (threadSync) {
            threadSync.notifyAll();
        }

    
public voidstart()
Start the background processing thread.

        threadData = endpoint.getConnectionHandler().init();
        thread = new ThreadWithAttributes(null, this);
        thread.setName(threadName);
        thread.setDaemon(true);
        thread.start();
    
public voidstop()
Stop the background processing thread.

        stopped = true;
        assign(null);
        thread = null;
        threadData = null;