Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it not encouraged to use threads in web application?

We've run into a big discussion with colleagues about using threads in web-applications in Java.

Their point is that it is not recommended to use threading in Java web applications because they are not managed by the container. Generally, I'm OK with this, because threads may interfere with the container. But, what should one use instead of it if, for example, it is not a Java EE application but a simple servlet-app?

like image 327
VasiliyL Avatar asked Jul 02 '12 11:07

VasiliyL


People also ask

Is it good to use thread in Web application?

We learn from the beginning that, thread is use to make parallel processing, but later when it come to web application development some suggest to never use thread in web application. i.e We have Checkmarx scanner and that suggest us to not thread in web application.

Is a Web application single threaded?

Most browsers have historically been single threaded (though that is changing rapidly: IE, Chrome, Firefox), and most JavaScript implementations occur in browsers.

Why do we need thread in programming?

Threads are very useful in modern programming whenever a process has multiple tasks to perform independently of the others. This is particularly true when one of the tasks may block, and it is desired to allow the other tasks to proceed without blocking.


1 Answers

Using threads in a webapp is not a problem per se. It just depends why and how you use them. In particular, if you have 1000 users, and you start one thread for each of these users, you'll bring the JVM to its knees.

But if threads are launched very raraely, for a specific, reduced set of users and use-cases, and if you use a thread pool to limit the number of such threads, you shouldn't have any problem. It's just important to understand what you're doing.

Also, make sure to not pass a HttpServletRequest or HttpServletResponse object to such a thread, because they aren't meant to be used by several concurrent threads, and they are not supposed to be used anymore once the request has been handled.

like image 106
JB Nizet Avatar answered Oct 08 '22 15:10

JB Nizet