• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets mutlithreading

 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • 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
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • 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).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64617
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I will work on that. Thanks all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic