I was taking a practice test, I came across the following question, in that ( HashMap localMap = new HashMap() decleared in doGet())correct answer was only localMap object is thread safe but other (hashmap)objects are not thread safe. can some please explain to why only localmap object is threads, my understanding was it is also not thread safe.
Consider the following code for a servlet:
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]