Java 多线程在 Web 应用程序中的作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20491879/
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 02:19:00  来源:igfitidea点击:

role of multithreading in web application

javamultithreadingservletsweb-applications

提问by Bhushan

I am using java(Servlets, JSPs) since 2 years for web application development. In those 2 years I never required to use multithreading(explicitly - as I know that servlet containers uses threading to serve same servlet to different requests) in any project.

2 年来,我一直在使用 java(Servlets, JSPs) 进行 Web 应用程序开发。在这 2 年里,我从未需要multithreading在任何项目中使用(明确地 - 因为我知道 servlet 容器使用线程来为不同的请求提供相同的 servlet)。

But whenever I attend an interview for Web Developer position(java), then there are several questions related to threads in java. I know the basics of java threading so answering the questions is not a problem. But sometimes I get confused whether I am missing something while developing web application by not using mutithreading?

但是每当我参加 Web 开发人员职位(java)的面试时,都会有几个与 java 中的线程相关的问题。我知道java线程的基础知识,所以回答问题不是问题。但有时我会感到困惑,我在不使用多线程开发 Web 应用程序时是否遗漏了什么?

So my question is that what is the role of multithreadingin Web Application? Any example where multithreadingcan be used in web application will be appreciated.

所以我的问题是multithreading在 Web 应用程序中的作用是什么?任何multithreading可以在 Web 应用程序中使用的示例将不胜感激。

Thanks in advance.

提前致谢。

采纳答案by SiN

Multi-threading can be used in Web Apps mainly when you are interested in asynchronous calls.

当您对异步调用感兴趣时,可以在 Web Apps 中使用多线程。

Consider for example you have a Web application that activates a user's state on a GSM network (e.g activate 4G plan) and sends a confirmatory SMS or email message at the end.

Knowing that the Web call would take several minutes - especially if the GSM network is stressed - it does not make sense to call it directly from the Web thread.

例如,假设您有一个 Web 应用程序,它激活用户在 GSM 网络上的状态(例如激活 4G 计划)并在最后发送确认短信或电子邮件消息。

知道 Web 呼叫需要几分钟的时间 - 特别是在 GSM 网络压力很大的情况下 - 直接从 Web 线程调用它是没有意义的。

So basically, when a user clicks "Activate", the Server returns something like "Thanks for activating the 4G plan. Your plan will be activated in a few minutes and you will receive a confirmation SMS/email".

所以基本上,当用户点击“激活”时,服务器会返回类似“感谢您激活 4G 计划。您的计划将在几分钟内激活,您将收到确认短信/电子邮件”。

In that case, you server has to spawn a new thread, ideally using a thread pool, in an asynchronous manner, and immediately return a response to the user.

在这种情况下,您的服务器必须以异步方式生成一个新线程,理想情况下使用线程池,并立即向用户返回响应。

Workflow:

工作流程:

1- User clicks "Activate" button
2- Servlet receives request and activates a new "Activate 4G Plan" task in a thread pool.
3- Servlet immediately returns an HTML response to the user without waiting for the task to be finalized.
4- End of Http transaction
. . .

1- 用户单击“激活”按钮
2- Servlet 接收请求并在线程池中激活新的“激活 4G 计划”任务。
3- Servlet 立即向用户返回 HTML 响应,而无需等待任务完成。
4- Http 事务结束
。. .

Asynchronously, the 4G plan gets activated later and the user gets notified through SMS or email, etc...

异步地,4G计划稍后激活,并通过短信或电子邮件等方式通知用户......

回答by TwoThe

Speaking about a real-world example, there are several reasons to use multi-threading, and I wouldn't hire a web-developer who doesn't know about it. But in the end, the reasons to use multi-threading are the same for standard- and web-development: you either want something that take a while (aka blocking) done in the background to give the user some response in between, or you have a task that can be speed up by having it run on several cores. When multi-threading is actually useful is however a different question.

说到一个现实世界的例子,使用多线程有几个原因,我不会聘请一个不了解它的 Web 开发人员。但最终,对于标准开发和 Web 开发而言,使用多线程的原因是相同的:您要么希望在后台完成一些需要一段时间(也称为阻塞)的事情,以便在两者之间给用户一些响应,要么您有一项可以通过在多个内核上运行来加速的任务。然而,多线程何时真正有用是一个不同的问题。

Situation 1:A web server that does require some processing and has low hits/second

情况 1:确实需要一些处理并且具有低命中率/秒的 Web 服务器

Here multi-threading (if applicable to the algorithm) is a good thing, as idle cores are utilized and threading can result in a faster response to the user.

在这里,多线程(如果适用于算法)是一件好事,因为利用了空闲内核,并且线程可以对用户产生更快的响应。

Situation 2:A web server that does require some processing and has high hits/second

情况 2:确实需要一些处理并且具有高命中率/秒的 Web 服务器

Here multi-threading is possible, but as cores are usually busy with other requests, there are no resources left to use it properly. Actually spreading out the task to several threads can even have a negative impact on the response time, as the task is now fragmented and all parts need to complete, but the order of execution with threads is undefined. So one client could immediately receive a response, while others might wait into time-out till their last fragment eventually gets processed.

多线程在这里是可能的,但由于内核通常忙于其他请求,因此没有资源可以正确使用它。实际上将任务分散到多个线程甚至会对响应时间产生负面影响,因为任务现在是碎片化的,所有部分都需要完成,但线程的执行顺序是不确定的。因此,一个客户端可以立即收到响应,而其他客户端可能会等待超时,直到他们的最后一个片段最终得到处理。

Situation 3:A web server has to do some processing that takes a very long time

情况3:Web服务器必须做一些需要很长时间的处理

Here multi-threading is required, there is no way around it. A client cannot wait minutes or probably hours till it receives the response. In this case a callback system is usually implemented, so basically each task has an "API" that can be queried for the current state. Most online-shops are an example for this: you order something and later you can query your order status.

这里需要多线程,没有办法绕过。客户端不能等待几分钟或几个小时才能收到响应。在这种情况下,通常会实现回调系统,因此基本上每个任务都有一个“API”,可以查询当前状态。大多数在线商店都是一个例子:您订购了一些东西,然后您可以查询您的订单状态。

The alternative to threading is process-forking, as Apache does in its standard configuration. The benefit is that load is spread across cores (mostly applicable to situation 2), and the web-code itself doesn't have to do anything to use all those cores, as the OS handles that automatically. However if you have imbalanced load, some cores can be idle and resources are not used in an optimal way. A threading situation is almost always the better solution, if it is done right. But the Apache/Tomcat standard configuration uses a very outdated threading model, by spawning one thread for each request. Effectively given a certain amount of hits/second, the CPU is more busy with threading than with actually processing those requests.

线程的替代方案是进程分叉,正如 Apache 在其标准配置中所做的那样。好处是负载分布在内核之间(主要适用于情况 2),并且 Web 代码本身无需执行任何操作即可使用所有这些内核,因为操作系统会自动处理这些内容。但是,如果您的负载不平衡,则某些内核可能处于空闲状态并且资源未以最佳方式使用。如果处理得当,线程处理几乎总是更好的解决方案。但是 Apache/Tomcat 标准配置使用一种非常过时的线程模型,为每个请求生成一个线程。有效地给定一定数量的命中/秒,CPU 处理线程比实际处理这些请求更忙。

回答by Anurag Sharma

Well this is a nice question and I think most of the developers who work in web application development don't use multithreading explicitly. The reason is quite obvious since you are using a application server to deploy your application, the application server internally manages a thread pool for incoming requests.

嗯,这是一个很好的问题,我认为大多数从事 Web 应用程序开发的开发人员并没有明确使用多线程。原因很明显,因为您使用应用程序服务器来部署应用程序,应用程序服务器在内部管理传入请求的线程池。

Then why use multithreading explicitly? What is need a web application developer expose himself to multithreading?

那为什么要显式地使用多线程呢?Web 应用程序开发人员需要什么才能接触多线程?

When you work on a large scale application where you have to server many request concurrently it is difficult to serve every kind of request synchronously because particular kind of request could have been doing a lot processing which could bring down the performance your application.

当您处理必须同时处理许多请求的大型应用程序时,很难同步处理每种类型的请求,因为特定类型的请求可能会进行大量处理,这可能会降低应用程序的性能。

Lets take an example where a web application after serving particular kind of request has to notify users through email and SMS. Doing it synchronously with the request thread could bring down the performance of your web application. So here comes the role of mutlithreading.In such cases it is advisable to develop a stand alone multithreaded application over the network which is responsible for sending email and SMS only.

让我们举一个例子,在处理特定类型的请求后,Web 应用程序必须通过电子邮件和 SMS 通知用户。与请求线程同步执行此操作可能会降低 Web 应用程序的性能。所以多线程的作用就来了。在这种情况下,建议通过网络开发一个独立的多线程应用程序,该应用程序仅负责发送电子邮件和 SMS。

回答by Kun

Multi-treading in web application can be used when you are interested in parallel action, e.g., fetching data from multiple addresses.

当您对并行操作感兴趣时,可以使用 Web 应用程序中的多线程,例如,从多个地址获取数据。

As I understand, multi-threading is used in different situation from thread-pool, which can be used to handle requests from multiple clients.

据我了解,多线程用于与线程池不同的情况,线程池可用于处理来自多个客户端的请求。