Is servlet is thread-safe?

Showing Answers 1 - 11 of 11 Answers

manish kumar

  • Mar 2nd, 2006
 

No,

Each servlet is not thread safe

  Was this answer useful?  Yes

No. Servlets are not Thread safe

The servlet is allows to access more than one threads at a time

if u want to make it Servlet as Thread safe ., U can go for

Implement SingleThreadInterface(i) which is a blank Interface there is no

methots

or we can go for synchronize methods

we can make whole service method as synchronized by using synchronized

keword in front of method

Ex::

public Synchronized class service(ServletRequest request,ServletResponse response)throws ServletException,IOException

or we can put block of the code in the Synchronized block

Ex::

Synchronized(Object)

{

----Instructions-----

}

I feel that Synchronized block is better than making the whole method

Synchronized

  Was this answer useful?  Yes

jhansi012

  • Mar 16th, 2006
 

Servlet is not thread-safe by itself. You can make it thread-safe by making the service method synchronized, but this reduces the performance, as the servlet cannot process the request from other user until the service method is finished. The best way to keep your unsafe code thread-safe is keep the few lines of code inside synchronized block.

Pawan Kumar Shrivastava

  • Mar 30th, 2006
 

The problem with the synchronized block / method is, the order of execution of threads is not guaranteed.

And also some servlet container may choose to invoke multiple instance of servlet or maintain a pool instead of creating threads. So it may not behave similar in all the containers.

srinivas

  • Dec 1st, 2006
 

servlet is thread safe for requests,but for the individual reqs not for all reqs

  Was this answer useful?  Yes

Neha

  • Nov 18th, 2007
 

servlet is not thread safe by itself but u can make it thread safe by synchronizing the particular line's of service method.

PriyankN

  • Mar 27th, 2011
 

Servlet is not thread safe, but that is because of the class/instance variables. So care must be taken to provide synchronized access to class/instance variables. Synchronizing entire service or doGet/doPost methods is bad and should be avoided.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions