Threads generated in Tomcat
I hav开发者_Python百科e a basic question on Tomcat thread creation. Does every browser instance run on a single thread or does it spawn multiple threads to process a single browser instance request?
I am taking a reference of the current thread in the code and calling the activecount method and it is showing 20 which indicates 20 active threads. So I have doubt from where this value is configured. Is there any parameter to set the active threads per
while (iter.hasNext()) {
GrammarSection agrammarSection= null;
try {
agrammarSection = (GrammarSection) iter.next();
} catch (Exception e) {
System.out.println("DDD if it come in exception "+Thread.currentThread());
System.out.println("DDD if it come in exception "+Thread.activeCount()); //IT PRINTS 20
Tomcat creates a pool of threads. Typically, one HTTP request is served by one thread.
Tomcat (and most servlet containers) use thread pools. That is - they pre-initialize a configurable number of threads, and whenever a request comes to the server, a thread is taken from the pool and assigned to handle the request.
Tomcat uses a thread pool, see this link for a brief overview of the config:
Default Tomcat Connector Behaviour
In response to "Does every browser instance run on a single thread?" the answer is "depends":
A single HTTP request that returns text etc. will consume one acceptor thread.
However, if your rendered page includes images too (on the same server instance) or if it uses frames then the browser will make requests for them too (because every image / page will require another HTTP request to the server).
And... the above relates to HTTP connector threads. You can, of course, have a servlet that is multi-threaded (to perform some arbitrary task). This won't count against the "maxThreads" limit seen in the configuration above, but will show as active threads in the JVM.
In general it really is not.
Many in the discussion here talked that 1 HTTP Request will be served by 1 Tomcat thread. But you should not think that 1 page will only trigger 1 HTTP Request.
At least it depends on which browser you are using, how many resources in 1 page, and whether there is AJAX involved, keep alive connections.
(1) One browser instance will spawn multiple thread in the browser/client side to download resources for a single page, and will occupy multiple threads on the Tomcat/Apache/Weblogic/Websphere/whatever application severs you use. This is due to the nature of modern multi-threaded browser. If you are talking really about plain HTML page, it could spawn only 1 (worker) thread in Tomcat, but when you add other resources in the page such as images, the images may be (and most likely) will be downloaded together with the page. Browser won't usually wait until the whole page loaded before loading images. You can see this clearly when you use tools such as FireBug (on the Net section). Pages are not loaded sequentially.
(2) In AJAX application, one page will trigger multiple threads on the servers as well.
(3) Take also into account that the HTTP 1.1 protocol (unless you are still using HTTP 1.0) will hold the connection until HTTP time out. It won't by default close the connection. You need to look at the Keep-Alive parameter in your Tomcat/WAR setting. You may need to add a Reverse Proxy (e.g. Apache, nginx, Squid, Varnish) in front of Tomcat to offload some of these keep-alived connections.
精彩评论