Question ID :996694346321
Consider the following code for a
servlet:
//[code]
import java.util.*;
public class TestServlet extends HttpServlet
{
static HashMap staticMap = new HashMap();
HashMap theMap = new HashMap();
public void init()
{
}
public void service(HttpServletRequest req, HttpServletResponse res)
{
super();
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
{
HashMap localMap = new HashMap();
//do something
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
HashMap sessionMap = (HashMap) req.getSession().getAttribute("map");
//do something
}
}
//[code]
Which of the statments regarding the HashMap objects are correct?
1.All the HashMap variables are
thread safe.
2. None of the hashMap variables are thread safe.
3.localMap is thread safe.
4.sessionMap is thread safe.
5.staticMap and theMap are thread safe
tha correct ans given is 3.
I want to know why sessionMap and theMap are not thread safe? as sessionMap is local for the doPost method.
thanks in advance..
Trupti