多线程在Web应用程序中的作用 [英] role of multithreading in web application
问题描述
自2年以来,我一直在使用java(Servlets,JSPs)进行Web应用程序开发.在那两年中,我从不需要在任何项目中使用multithreading
(明确地-我知道servlet容器使用线程为不同的请求提供相同的servlet).
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.
但是,每当我参加Web开发人员职位(java)的面试时,就会有几个与java中的线程相关的问题.我知道Java线程的基础知识,因此回答问题不是问题.但是有时候我会因为不使用mutithreading而在开发Web应用程序时是否缺少某些东西感到困惑?
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?
所以我的问题是multithreading
在Web应用程序中的作用是什么?可以在Web应用程序中使用multithreading
的任何示例.
So my question is that what is the role of multithreading
in Web Application? Any example where multithreading
can be used in web application will be appreciated.
谢谢.
推荐答案
主要在对异步调用感兴趣的情况下,可以在Web Apps中使用多线程.
Multi-threading can be used in Web Apps mainly when you are interested in asynchronous calls.
例如,请考虑您有一个Web应用程序,该应用程序可以激活GSM网络上的用户状态(例如,激活4G计划),并在最后发送确认的SMS或电子邮件消息.
知道Web调用将花费几分钟,尤其是在GSM网络压力很大的情况下,直接从Web线程调用它是没有意义的.
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.
因此,基本上,当用户单击激活"时,服务器将返回类似感谢激活4G计划.您的计划将在几分钟内被激活,并且您会收到确认的SMS/电子邮件".
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".
在这种情况下,您的服务器必须以异步方式生成一个新线程,最好使用线程池,然后立即将响应返回给用户.
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.
工作流程:
1-用户单击激活"按钮
2- Servlet接收请求并在线程池中激活新的激活4G计划"任务.
3- Servlet立即向用户返回HTML响应,而无需等待任务完成.
4- Http事务结束
.
.
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
.
.
.
异步地,稍后启动4G计划,并通过SMS或电子邮件等方式通知用户...
Asynchronously, the 4G plan gets activated later and the user gets notified through SMS or email, etc...
这篇关于多线程在Web应用程序中的作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!