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?
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.
Most browsers have historically been single threaded (though that is changing rapidly: IE, Chrome, Firefox), and most JavaScript implementations occur in browsers.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With