• 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

Servlets mutlithreading

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have looked through the documentation and have found 2 methods for multithreading servlet methods, (1)synchronizing (2)singlethreadmodel. (1) could cause deadlocking and congestion, (2) has been deprecated as of 2.4. Are there other approaches?
Thanks
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Certainly. Just write good code. Keep in mind that there is only one instance of the servlet in application's lifetime which is to be (re)used among all requests. So whenever you use instance variables, then those won't be threadsafe. But if you use method local variables, those will be threadsafe. Whether threadsafety has impact depends on the variable in question. If the variable is intented to hold request or session scoped values (e.g. request parameters, logged in user, etc), just declare them in method block. If the variable is intented to hold application scoped values (e.g. the DAO class, some constants, etc), then you can safely declare it as instance variable.

There's absolutely no need for synchronizing or SingleThreadModel if you write correct code (that's also one of the reasons why SingleThreadModel is deprecated).
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the need? It's usually sufficient to ensure that your servlets are written in a thread-safe manner.

I've never had to really upon any multi-threading except for that already provided by the container. If you express what you're trying to accomplish perhaps a solution could be ventured?
 
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
The best approach?

Thoroughly understand what the servlet container provides for you.

Always keep in mind that servlets work on a request/response basis.

Remember there is only one instance of a servlet potentially working on multiple requests "at one time" so pay attention to the scope of variables.

Bill
 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will work on that. Thanks all.
 
reply
    Bookmark Topic Watch Topic
  • New Topic