Forums Register Login

Thread safe

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
Normally trees don't drive trucks. Does this tiny ad have a license?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 917 times.
Similar Threads
Mock Question
question from the jweb+
Single Thread Model
thread safe
Thread safe Please help!!!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:44:08.