This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes question from the jweb+ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "question from the jweb+ " Watch "question from the jweb+ " New topic
Author

question from the jweb+

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
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
Adrian Muscalu
Ranch Hand

Joined: May 08, 2000
Posts: 73
sessionMap is not safe because objects stored into the session are not thread safe. You are not creating a new object sessionMap, you are just pointing to an existing object which is not thread safe.
theMap is not safe because multiple threads can access your servlet in the same time. If one instance modifies theMap then all other instances will be having the new changes. There are different ways to avoid this.
Regards,
Adrian
[ November 08, 2002: Message edited by: Adrian Muscalu ]

SCJP2, SCWCD
friso jonge
Ranch Hand

Joined: Aug 06, 2002
Posts: 41
some extra info.
session variables are not completely threadsafe. This is explained very well in threads with peter den haan....
E.g. consider if a person opens another browser and copies the link into the url. Consider multiple frames etc. That sort of scenarios. Find a good explanation from peter if you care.
cheers
Timber Lee
Ranch Hand

Joined: Oct 14, 2002
Posts: 157
why is the localMap thread safe?


SCJP<br />SCWCD<br />SCEA
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
localMap is threadsafe because every thread executing doGet() will create its own localMap. In other words, every copy of localMap is accessible to just a single thread -- and when there's just one thread to contend with, everything is threadsafe
Session data is often treated as if there were no threading issues with it. Don't. This practice will come back to haunt you sooner or later. In my last project, I had to patch up someone else's code because of this; it had served us beautifully for over a year, but this time round it caused the occasional weird glitch. Probably because the project was heavily frame-based, and browsers like to request several frames at once, giving you several threads accessing the same session. It was a good thing that I had been aware of the thread unsafety for a while and didn't have to spend hours trying to track down what was a hard to reproduce but serious problem.
- Peter
Suresh Babu Venkat
Ranch Hand

Joined: Sep 15, 2009
Posts: 30
Adrian Muscalu wrote:sessionMap is not safe because objects stored into the session are not thread safe. You are not creating a new object sessionMap, you are just pointing to an existing object which is not thread safe.
theMap is not safe because multiple threads can access your servlet in the same time. If one instance modifies theMap then all other instances will be having the new changes. There are different ways to avoid this.
Regards,
Adrian
[ November 08, 2002: Message edited by: Adrian Muscalu ]



public void doPost(HttpServletRequest req, HttpServletResponse res)
{
HashMap sessionMap = (HashMap) req.getSession().getAttribute("map");
//do something
}



in the above Quote sessionMap is also a local variable right?
just that its been passed a value of session atttribute.

As java is pass by value, i think sessionMap would be not at all linked to session attribute "map" once its been assigned to sessionMap.

Can anybody clear my understanding?


SCJP 6.0 (88%) | SCWCD 5.0 (89%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question from the jweb+
 
Similar Threads
Mock Question
thread safe
Question ID :995549757220
servlet questin
Thread safe