• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

question from the jweb+

 
Ranch Hand
Posts: 647
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is the localMap thread safe?
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic