`
michaelljx
  • 浏览: 8867 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Tomcat的HttpConnector和HttpProcessor线程交互

    博客分类:
  • Web
 
阅读更多

The processor thread (the await method)

while (!available) { wait(); }

Socket socket = this.socket;

available = false; notifyAll();

return socket;

 

 

The connector thread (the assign method)

while (available) { wait(); }

this.socket = socket;

available = true;

notifyAll();

 

Initially, when the "processor thread" has just been started, available is false, so the thread waits inside the while loop (see Column 1 of Table 4.1). It will wait until another thread calls notify or notifyAll. This is to say that calling the wait method
causes the "processor thread" to pause until the "connector thread" invokes the notifyAll method for the HttpProcessor instance. Now, look at Column 2. When a new socket is assigned, the "connector thread" calls the HttpProcessor's assign method. The value of available is false, so the while loop is skipped and the socket is assigned to the HttpProcessor instance's socket variable: this.socket = socket;
The "connector thread" then sets available to true and calls notifyAll. This wakes up the processor thread and now the value of available is true so the program controls goes out of the while loop: assigning the instance's socket to a local variable, sets available to false, calls notifyAll, and returns the socket, which eventually causes the socket to be processed. Why does the await method need to use a local variable (socket) and not return the instance's socket variable? So that the instance's socket variable can be assigned to the next incoming socket before the current socket gets processed completely. Why does the await method need to call notifyAll? Just in case another socket arrives when the value of available is true. In this case, the "connector thread" will stop inside the assign method's while loop until the nofifyAll call from the "processor thread" is received.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics