IntelliJ Java IDE
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Thread safe Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "Thread safe" Watch "Thread safe" New topic
Author

Thread safe

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi!
In the below 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
}
}
I believe that the request attributes and LOCAL OBJECTS are thread safe but sessionMap is not thread safe. Why??
Thank you in advance.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Well, what is a "local object"? A local object is not an object reference held in a local variable, but an object which is only locally reachable.
The object referred to by the sessionMap variable is session-scoped, i.e. bound as an HttpSession attribute. This makes it non-locally reachable. Because a single session may easily experience multiple simultaneous requests -- each of which probably gets its own thread -- the object is (in principle) not safe against multi-threaded access.
Multiple concurrent requests for the same session are a common occurrence with frames, dynamically rendered graphics or other embedded/included page elements, users with no patience for servers under load, etc.
- Peter
[ April 13, 2002: Message edited by: Peter den Haan ]
 
 
subject: Thread safe
 
Threads others viewed
thread safe
Mock Question
Single Thread Model
Thread safe Please help!!!
question from the jweb+
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com