Tomcat 线程与 Java 线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2810054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:05:58  来源:igfitidea点击:

Tomcat threads vs Java threads

javamultithreadingtomcat

提问by black666

When using java threads, one has to take care of the basic problems that come with concurrency through synchronization etc.

使用 Java 线程时,必须通过同步等处理并发性带来的基本问题。

AFAIK Tomcat also works with threads to handle its workload. Why is it, that I don't have to think about making my code threadsafe when it is running in Tomcat?

AFAIK Tomcat 还使用线程来处理其工作负载。为什么我在 Tomcat 中运行时不必考虑使我的代码线程安全?

采纳答案by skaffman

You dohave to make your code thread safe in tomcat. Tomcat will invoke your code (i.e. your servlets) from multiple threads, and if that code is not thread-safe, you'll have problems.

必须让你的代码的线程在Tomcat中是安全的。Tomcat 将从多个线程调用您的代码(即您的 servlet),如果该代码不是线程安全的,您就会遇到问题。

Tomcat's threads are no different to any threads you create yourself.

Tomcat 的线程与您自己创建的任何线程没有什么不同。

回答by matt b

To add on to what skaffman has mentioned, it might seem like you don't need to think about multi-threading when writing a webapp because the Servlet framework/API is oriented completely around implementing methods (service(), doGet(), doPost(), etc) which are invoked once per HTTP request.

要添加到什么skaffman已经提到的,它可能看起来像你不需要写一个web应用程序时,考虑多线程,因为Servlet的框架/ API是面向完全围绕实现方法(service()doGet()doPost()等),这些调用一次每个 HTTP 请求。

Therefore, in a simple application, you can implement these methods in your servlet and/or JSP or whatever and not think about what happens when multiple threads interact.

因此,在一个简单的应用程序中,您可以在您的 servlet 和/或 JSP 或其他任何东西中实现这些方法,而不必考虑多个线程交互时会发生什么。

But the second you start having shared state between servlets or service methods, then without possibly realizing it you aredealing with multiple threads interacting, and if you aren't careful, you will eventually have multi-threading or synchronization issues. You will have to deal with this because in Tomcat (and I assume all servlet containers, although I don't know if it's required by the Servlet spec) each request is handled by (possibly) a different thread. Therefore if you receive two simultaneous requests, these will be handled by two separate threads concurrently (at the same time).

但是,当您开始在 servlet 或服务方法之间共享状态时,可能没有意识到您正在处理多个线程交互,如果您不小心,您最终会遇到多线程或同步问题。您将不得不处理这个问题,因为在 Tomcat 中(我假设所有 servlet 容器,尽管我不知道 Servlet 规范是否需要它)每个请求都由(可能)不同的线程处理。因此,如果您同时收到两个请求,这些请求将同时(同时)由两个单独的线程处理。

回答by duffymo

Because Java EE containers are written in such a way that they handle the threading for you. You write your code to be thread-safe and the container does the rest. It pools threads and assigns them one per request as they come in.

因为 Java EE 容器的编写方式可以为您处理线程。您将代码编写为线程安全的,其余的由容器完成。它汇集线程并在每个请求进入时为其分配一个。

回答by Adam Gent

If you think that Tomcat makes your application thread safe write a Servlet with mutable member variables like a non-concurrent hashmap.

如果您认为 Tomcat 使您的应用程序线程安全,请编写一个具有可变成员变量(如非并发哈希图)的 Servlet。

Then have the servlet put things in that hashmap for every request. It won't take long to get a lovely concurrency exception.

然后让 servlet 为每个请求将内容放入该哈希图中。很快就会得到一个可爱的并发异常。

This is why in general for singleton-like components you have to be very careful with member variables because they are shared between multiple threads accessing the object.

这就是为什么通常对于类似单例的组件你必须非常小心成员变量,因为它们在访问对象的多个线程之间共享。

Now the servlet container create a new transient object for every request (which is what some web app frameworks do) you could put behavior that interacted with the member variables in that transient object and be thread safe.

现在 servlet 容器为每个请求创建一个新的瞬态对象(这是某些 Web 应用程序框架所做的),您可以将与成员变量交互的行为放入该瞬态对象中,并且是线程安全的。