tomcat是否为每个用户创建一个线程? [英] Does tomcat create a thread per user?

查看:309
本文介绍了tomcat是否为每个用户创建一个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对网络开发还很陌生。如果这是一个非常基本的问题,我很抱歉。例如,我创建一个Web应用程序并将其部署到tomcat。现在,当多个用户访问Web应用程序时,tomcat是否为每个用户创建了一个新线程?如果是这种情况,那么我仍然可以在我的应用程序本身中创建线程并期望它保持在tomcat创建的每个用户线程的本地吗?会话级数据是否在线程之间保持同步?

I am fairly new to web development. So I apologize if this is a very basic question. For example, I create a web application and deploy it to tomcat. Now when multiple users hit the web application, does tomcat create a new thread per user? If that's the case then can I still create threads in my application itself and expect it to stay local to each user thread created by tomcat? Does session level data stay synchronized across threads?

我希望我的问题有意义。

I hope my question makes sense.

推荐答案

每个请求都在不同的线程中处理。这不是每个用户的线程。请求是来自客户端(Web浏览器)和服务器的任何交互。因此,在您的浏览器中键入Url,调用ajax请求,每个请求都在一个单独的线程中处理。

Each request is handled in a different thread. This is not a "thread per user". A request is any interaction from the client (web browser) and the server. So, typing a Url in you browser, invoking an ajax request, each one is handled in a separate thread.

用户在登录期间获得的状态(它本身不必是登录;更好的方式是说它是一组相关的一个用户)的请求被方便地存储在会话中。您可以使用会话来存储适用于用户的任何数据,但是您应该注意不要存储太多数据,因为它会占用内存。会话管理需要一定程度的技能。

The state a user acquires during a 'login' (it doesn't have to be a login per se; a better way to say it is "a set of related requests by one user") is conveniently stored in the session. You can use the session to store any data that is applicable to the user, although you should be careful not to store too much data because it eats up memory. Session management requires a degree of skill.

是的,如果你发起新线程,你必须非常小心;你可以破坏事物,通常是一个坏主意。如果必须执行需要很长时间的操作,请使用JMS异步处理它。还要记住,并非所有影响Web应用程序数据的任务都必须从webapp调用。每天扫描数据的任务可以作为一个单独的任务进入或运出tomcat - 即你可以使用石英调度程序编写一个工作,或者甚至编写一个单独的程序并将其设置为在cron中运行(be小心改变你webapp下的数据的工作,但是)。

Yes you must be very careful if you fire off new threads; you can break things, and typically its a bad idea. If you must do something that will take a long time, use JMS to handle whatever it is asynchronously. Also remember that not all tasks that affect web application data have to invoked from the webapp. A task that scans the data daily can be run as a separate task in or out of tomcat -- i.e. you can write a job using something like quartz scheduler, or even write a separate program and set it up to run in a cron (be careful of the job changing the data from under you webapp, though).

如果你使用的是Spring和Hibernate等最好的技术,它们通常会绑定对象(或者可以由应用程序开发人员配置程序员将需要每个线程(使用java的ThreadLocal)。

If you are using best of breed technologies such as Spring and Hibernate, they typically bind objects (or can be configured by the app developer) that the programmer will need to each thread (using java's ThreadLocal).

这也是启动自己的线程危险的原因之一。如果您启动自己的线程,则在请求结束时可能会丢失绑定到初始线程的资源,这意味着如果您尝试在工作线程中访问这些资源,则它们将无法使用。这种类型的错误可能会让人难以找到/修复。

This is also one of the reasons starting your own threads is dangerous. If you start your own thread, you might lose resources that are bound to the initial thread when the request ends, and this means that if you try to access those resources in your worker thread they wont be available. This type of bug can be a pain in the ass to find/fix.

编辑 - 正如Stephen C在评论中指出的另一个答案,重要的是要注意通常Tomcat(和其他容器)维护一个线程池供使用。这意味着不一定为每个请求创建新线程。这意味着每个请求在一个单独的线程中运行,该线程可能会也可能不会被创建或重用。

edit - as Stephen C pointed out in a comment for another answer, its important to note that typically Tomcat (and other containers) maintain a pool of threads for use. This means that a new thread is not necessarily created for every request. It means that each request runs in a separate thread, which might or might not be created or reused.

这篇关于tomcat是否为每个用户创建一个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆