Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

which part of the servlet runs in new threads

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

in Head First servlets and jsp book it was given like this .A seperate thread is created on every request, and before service() method called a seperate thread is created and control is transferred to service() method().

Does this mean remaining parts of servlet are shared by multiple threads
simultaniously ?

If I declare a HashMap in a servlet class as an instance variable then will it be shared by multiple threads .

I refer this HashMap in doPost()


eg





please tell in the above example if the HashMap h is shared by multiple threads .Is this the proper place to declare a HashMap in a servlet.



my application requirement after upload keep track of all uploaded file in a HashMap .So that when a user tries to delete using a link in the html page servlet shud delete that file only in the file system.
[ December 16, 2008: Message edited by: vijaya saradhi ]
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right that the HashMap will be shared by multiple threads. I don't think you can face problems with the specification of your code. But I am thinking about a case where two files with the same name are uploaded at the same time. There might be some more cases which I am not able to figure out . So for safety, I think you must synchronize the doPost method.
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables are shared by all threads. Since that HashMap is mutable (there's a "put" method), this code is not thread-safe. You need to synchronize access to it.

The doGet and doPost methods are called from the service method, so both can be run by multiple threads/requests simultaneously.
[ December 17, 2008: Message edited by: Ulf Dittmer ]
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic