File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from the jweb+

 
trupti nigam
Ranch Hand
Posts: 623
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
friso jonge
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 157
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is the localMap thread safe?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic