---
title: "Tomcat源码分析（三）------ 可携带状态的线程池"
author: "wxyfighting"
date: "2013-06-10T13:02:00Z"
tags: []
slug: "tomcat"
draft: false
lastmod: "2013-06-12T14:33:39Z"
id: 1885628
url: "https://wxyfighting.iteye.com/blog/1885628"
---

最近想实现一个可携带状态的线程池，具体需求就是池中的线程被用来处理某种信息，而此信息可视为线程所依赖的外部状态。如果用简单的线程池来实现，线程初始化时就得赋予某些信息，使得线程无法被再次利用。在看老版Tomcat的源码时，找到了答案，其实现思路主要是利用了线程的等待和唤起，HttpProcessor的实现正好基于此思路，时序图如下所示：

![](http://p.blog.csdn.net/images/p_blog_csdn_net/cutesource/EntryImages/20091226/httpProcessor.jpg)

初始化HttpProcessor线程时，没法赋予所需的Socket对象，因为如果在初始化阶段就赋予Socket会导致此线程没法回收用来处理其他Socket。因此，在HttpProcessor的run阶段，先把线程给wait住，具体在await方法里体现，代码如下所示：

**\[java]**[view plain](http://blog.csdn.net/cutesource/article/details/5081916 "view plain")[copy](http://blog.csdn.net/cutesource/article/details/5081916 "copy")

01. /\**
02. \*AwaitanewlyassignedSocketfromourConnector,or&lt;code&gt;null&lt;/code&gt;
03. \*ifwearesupposedtoshutdown.
04. \*/
05. privatesynchronizedSocketawait(){
06. //WaitfortheConnectortoprovideanewSocket
07. while(!available){
08. try{
09. wait();
10. }catch(InterruptedExceptione){
11. }
12. }
13. //NotifytheConnectorthatwehavereceivedthisSocket
14. Socketsocket=this.socket;
15. available=false;
16. notifyAll();
17. if((debug&gt;=1)&&(socket!=null))
18. log("Theincomingrequesthasbeenawaited");
19. return(socket);
20. }

当HttpConnector调用HttpProcessor.assign(socket)方法时，会给此线程赋予Socket对象，并唤起此线程，使其继续执行，assign方法的源码如下所示：

**\[java]**[view plain](http://blog.csdn.net/cutesource/article/details/5081916 "view plain")[copy](http://blog.csdn.net/cutesource/article/details/5081916 "copy")

01. /\**
02. \*ProcessanincomingTCP/IPconnectiononthespecifiedsocket.Any
03. \*exceptionthatoccursduringprocessingmustbeloggedandswallowed.
04. \*&lt;b&gt;NOTE&lt;/b&gt;:ThismethodiscalledfromourConnector'sthread.We
05. \*mustassignittoourownthreadsothatmultiplesimultaneous
06. \*requestscanbehandled.
07. \*
08. \*@paramsocketTCPsockettoprocess
09. \*/
10. synchronizedvoidassign(Socketsocket){
11. //WaitfortheProcessortogetthepreviousSocket
12. while(available){
13. try{
14. wait();
15. }catch(InterruptedExceptione){
16. }
17. }
18. //StorethenewlyavailableSocketandnotifyourthread
19. this.socket=socket;
20. available=true;
21. notifyAll();
22. if((debug&gt;=1)&&(socket!=null))
23. log("Anincomingrequestisbeingassigned");
24. }

线程被唤起和赋予socket对象后，继续执行核心的process方法，HttpProcessor.run的完整源码如下所示：

**\[java]**[view plain](http://blog.csdn.net/cutesource/article/details/5081916 "view plain")[copy](http://blog.csdn.net/cutesource/article/details/5081916 "copy")

01. /\**
02. \*ThebackgroundthreadthatlistensforincomingTCP/IPconnectionsand
03. \*handsthemofftoanappropriateprocessor.
04. \*/
05. publicvoidrun(){
06. //Processrequestsuntilwereceiveashutdownsignal
07. while(!stopped){
08. //Waitforthenextsockettobeassigned
09. Socketsocket=await();
10. if(socket==null)
11. continue;
12. //Processtherequestfromthissocket
13. try{
14. process(socket);
15. }catch(Throwablet){
16. log("process.invoke",t);
17. }
18. //Finishupthisrequest
19. connector.recycle(this);
20. }
21. //TellthreadStop()wehaveshutourselvesdownsuccessfully
22. synchronized(threadSync){
23. threadSync.notifyAll();
24. }
25. }