• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which is thread safe ?

 
jawwad ahmed
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well while not implementing which is thread safe.
public class First extends HttpServlet
{
String str=new String("jawwad");//1
public void doGet(HttpServletRequest req,HttpServletResponse)
{
ServletConfig config=getServletConfig();//2
String s1=config.getInitParameter("one");//3
ServletContext context=getServletContext();//4
String s2=context.getInitParameter("two");//5
}
}
Select any two.Which reference assigned is thread safe.
 
Ian B Anderson
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
This seems very tough and I could be wrong but here goes...
1) is definetly not thread safe as it is an instance variable
2) I would say say this is thread safe because it is used to pass initialization params to a Servlet etc.
3) I would say 3 is safe because there is no setInitParameter so in theory this is the same while the server is running
4) I would say this isn't thread safe because it returns the ServletContext in which the event took place. Also stores info about the environment in which the Servlet is running. Different events so possible threading issues. (I could be wrong here!).
5) I would say this isn't thread safe because you get different SerletContexts on different VMs (Once again I could be wrong!)

So my final answer would be 2 and 3 are thread safe.
Ian
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic