Thread safety of the servlet



Q) How to ensure the thread safety of the servlet?

Rep)

1) It is better to avoid instance variables & static variables in a servlet class

2) If the instance variable reference to a threadsage object then there will be no problem

3) If all Instance variables refers to an object that is not thread safe then we most provide synchronize block as shown below.

public class Sone extends HttpServlet

ArrayList a1=new ArrayList();

public void service(–)throws — {

—-

synchronized(a1) {

–> In this case the out variable is an instance variable

al.add(—);

}

}   //Synchronized block must be written by a share code.

}

4) We must provide synchronized block while accessing thread unsafe objects that are stored as Attributes of Sessionobject and application object.

Random Posts

  • Define Session Bean and Entity Bean
  • when do use application scope
  • Define Array in Java
  • Services Provided by EJB Container
  • Servlet Interview Questions

Post a Comment