Abhijeet Panwar
Abhijeet Panwar

Reputation: 1857

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.

Upvotes: 6

Views: 3554

Answers (3)

Atul
Atul

Reputation: 2711

An excellent answer to a similar question is witten by BalusC here. Also have a look at Tomasz's answer

Generally, instance variables or state can be shared across threads (threads created by application or the container). So any class(object) that exposes its state for modification, can be considered unsafe. So if your service layer calls some data access object method and the dao is an instance variable inside the service class, the question to ask is this - can this dao or the state of that dao itself be changed by some other client?

  1. You can make your objects immutable. Your custom objects, dates and collections can be mutable. Some of the examples where even getter methods can be dangerous are collections, dates, etc. Use something like ConcurrentHashMap or return a list something like Collections.unmodifiablelist

Another example, instead of returning this.someDate, you should write public Date getSomeDate() { return new Date(someDate.getTime()); } This way some other thread (which may have been spawned by container for another request from another user) holding a reference to the variable someDate will not be able to mess up with this thread.

  1. If you cannot make the state of an object immutable because you want to allow its clients to change its state, you can make all the clients of that object agree to share the state. So if one thread changes the state of a shared object and another thread is ok with the state changed by the first thread, then such monostate object can be ok to have in your application.

As other answers have mentioned the container spawns threads even if your application does not. I have focused here mainly on the topics not directly covered in the answers here so as to avoid duplication. Hope this helps.

Upvotes: 1

meriton
meriton

Reputation: 70564

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.

Upvotes: 1

Braj
Braj

Reputation: 46841

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.

Upvotes: 7

Related Questions