HttpConnector 与 HttpProcessor 同步控制

来源:互联网 发布:mac压缩pdf文件大小 编辑:程序博客网 时间:2024/06/06 05:00

HttpConnector 与 HttpProcessor 同步控制

Tomcat学习 HttpConnector和HttpProcessor启动流程和线程交互

 

一些疑问

1、对于assign方法里面的:

  this.socket = socket;

和awai方法里面的:

Socket socket = this.socket;

难于理解。

就是:为什么 await 需要使用一个本地变量(socket)而不是返回实例的 socket 变量呢?

因为这样一来,在当前 socket 被完全处理之前,实例的 socket 变量可以赋给下一个前来的 socket 

 


 

------------HttpProcessor---------------------------

private boolean available = false; public HttpProcessor(HttpConnector connector, int id) {        super();        this.connector = connector;        this.debug = connector.getDebug();        this.id = id;        this.proxyName = connector.getProxyName();        this.proxyPort = connector.getProxyPort();        this.request = (HttpRequestImpl) connector.createRequest();        this.response = (HttpResponseImpl) connector.createResponse();        this.serverPort = connector.getPort();        this.threadName =          "HttpProcessor[" + connector.getPort() + "][" + id + "]";    } private synchronized Socket await() {        // 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();        if ((debug >= 1) && (socket != null))            log("  The incoming request has been awaited");        return (socket);    }-------------------public void run() {        // 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            process(socket);            // Finish up this request            request.recycle();            response.recycle();            connector.recycle(this);        }        // Tell threadStop() we have shut ourselves down successfully        synchronized (threadSync) {            threadSync.notifyAll();        }    }----------------synchronized void assign(Socket socket) {        // 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();        if ((debug >= 1) && (socket != null))            log(" An incoming request is being assigned");    }

 

 

-------------------HttpConnector-------------------

run(){// Loop until we receive a shutdown command        while (!stopped) {            // Accept the next incoming connection from the server socket            Socket socket = null;            try {                socket = serverSocket.accept();                if (connectionTimeout > 0)                    socket.setSoTimeout(connectionTimeout);            } catch (AccessControlException ace) {                log("socket accept security exception: " + ace.getMessage());                continue;            } catch (IOException e) {                if (started && !stopped)                    log("accept: ", e);                break;            }            // Hand this socket off to an appropriate processor            HttpProcessor processor = createProcessor();            if (processor == null) {                try {                    log(sm.getString("httpConnector.noProcessor"));                    socket.close();                } catch (IOException e) {                    ;                }                continue;            }            processor.assign(socket);            // The processor will recycle itself when it finishes        }        // Notify the threadStop() method that we have shut ourselves down        synchronized (threadSync) {            threadSync.notifyAll();        }}  -----------------       private HttpProcessor createProcessor() {        synchronized (processors) {            if (processors.size() > 0)                return ((HttpProcessor) 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, curProcessors++);        if (processor instanceof Lifecycle) {            try {                ((Lifecycle) processor).start();            } catch (LifecycleException e) {                log("newProcessor", e);                return (null);            }        }        created.addElement(processor);        return (processor);    }

 ...

 

 

0 0
原创粉丝点击