• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Concurrency Issue in Servlet

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How concurrency issues are solved in Servlet ?
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you will have to tell us a bit more about what you want to know.

The servlet API is by its nature concurrent. The service(..) and doXX(..) methods of a servlet (and thus any methods they call) can and will be executed by many threads at once, under the control of the servlet container.

Are you asking how to ensure that any servlet code you write is "thread safe" (also known as "reentrant") ?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kri,

I think, you must localize all your objects and instances that will solve the concurrency problem. For instance, define a separate class, containing all the objects you are using inside the servlet. Now instantiate it inside the servlet and access them through this instance. This should solve your concurrency problem, that normally we face.

Thanks and Regards
Dillip
 
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I first time read a solution like that Dilip. Is this widely used in the industry? I doubt this.

Anyone knows?

Regards,
Leena
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dillip,

What is the advantage of using an inner class defined in a servlet over instantiating some class defined in a separate file and "adding it" by using the setAttribute() method of ServletContext, ServletRequest or HttpSession objects?
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its up to the programmer to solve it.To avoid this you may want to implement the SingleThreadModel interface in your servlet. There is good article on that here.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Leena said:

I first time read a solution like that Dilip. Is this widely used in the industry? I doubt this.


Creating a class to hold user specific data is done all the time. For one thing, it makes things simpler than plugging various bits into the session and hauling them back out again. Note that we are NOT talking about putting everything you use in this class, just the user specific data. If you are using JSP, this class is an obvious candidate for the JavaBean interface.

Another (frequently un-appreciated) reason to create such a user-specific class is the ease of debugging. You can test it outside the servlet environment - a vast simplification.

Bill
(Avoid the SingleThreadModel like the plague - if you think you need it, your architecture is wrong.)
[ July 12, 2004: Message edited by: William Brogden ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic