Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread safety in java web application?

What does someone mean when I am asked that whether my web application is thread safe or not , considering that I have not used Multiple threads in my webapplication.

like image 380
Abhijeet Panwar Avatar asked Jan 11 '23 03:01

Abhijeet Panwar


2 Answers

In a normal web-application Servlet treats as Singleton class, it means if you are using instance variable in Servlet that is not thread safe in that case it will create an issue for multiple request that is served simultaneously.

A Java servlet container / web server is typically multithreaded. That means, that multiple requests to the same servlet may be executed at the same time. Therefore, you need to take concurrency into consideration when you implement your servlet.

Read more...

enter image description here

What does someone mean when I am asked that whether my web application is thread safe or not

You have to make sure that all the Servlet/JSP are thread-safe. Do it for all server side classes that is treated as Singleton.

I have not used Multiple threads in my webapplication.

Container/web server starts a new thread for each request.

like image 76
Braj Avatar answered Jan 12 '23 17:01

Braj


The servlet specification requires a web application to be thread safe, because the servlet container may (and usually does) process requests concurrently. That is, even if you do not start any threads of your own, the servlet container will, and you must ensure your code is still correct in that case.

That involves protecting any objects shared by several threads (such as the contents of the HttpSession, or any singleton objects) from concurrent access.

like image 44
meriton Avatar answered Jan 12 '23 18:01

meriton