Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

role of multithreading in web application

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.

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?

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.

Thanks in advance.

like image 789
Bhushan Avatar asked Dec 10 '13 10:12

Bhushan


People also ask

Is multithreading used in web development?

Yes, you can use multi-threading in web applications. In fact, most web servers (or web containers) by default operate in a multi-threaded model where for each new HTTP request a new thread is spawned to process the request and generate the respond. Let' take an example of Java Servlets.

What is a multi-threaded application?

A multi-threaded application is an application whose architecture takes advantage of the multi-threading provided by the operating system. Usually, these applications assign specific jobs to individual threads within the process and the threads communicate, through various means, to synchronize their actions.


4 Answers

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

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.

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.

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
. . .

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

like image 119
SiN Avatar answered Sep 25 '22 17:09

SiN


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.

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

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

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

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.

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.

like image 40
TwoThe Avatar answered Sep 26 '22 17:09

TwoThe


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.

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

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.

like image 31
Anurag Sharma Avatar answered Sep 23 '22 17:09

Anurag Sharma


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

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

like image 44
Kun Avatar answered Sep 27 '22 17:09

Kun