• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Thread-safe in Servlets

 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi....
By default, servlets are multithreaded,so Are they Thread-safe? and How?
 
C Shinde
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets are rightly not threadsafe, so you need to either implement SingleThreadModel which is deprecated in the latest servlet Spec. or you need to code your Servlet in such a way that it is Thread-safe.

You can refer to this article for the same :JavaWorld - ThreadSafe Servlets - A Simple example
 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C Shinde wrote:Servlets are rightly not threadsafe,


Thanks for replay,

so,why Java soft people implemented like this?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because for most servlets, the issue of thread safety doesn't arise. You design systems to work naturally for the common uses, but to allow programmers to do uncommon things. And since Java already has features which allow programmers to achieve thread safety when necessary, it wasn't necessary to do anything specific in the implementation of servlets. It's the programmer's responsibility to deal with it in the few cases where it is an issue.
 
C Shinde
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this URL at CodeRanch - Code Ranch .

Well, as I said there was this SingleThreadModel that could be implemented to make threadsafe, but was deprecated as it did not providing complete synchronization, it provided code level sync but then the session context, applicationcontext was not synchronized.

So the other option is the only way to get thread-safety in servlets is to code the Servlet in a Thread-Safe manner. THe JavaWorld guys have told how to do this.

You can try reading on net of what Thread-Safety means. Which you can again read here. webpage.
 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Because for most servlets, the issue of thread safety doesn't arise.


Hi Paul,
Thanks for replay,

But still I have little bit of doubt regarding servlets thread safety, so why most servlets, the issue of thread safety doesn't arise.How can you say this? and what is reason behind this? please clarify my doubt.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue of Thread safety does not arise because each request is handled by a single Thread and this thread is the only one that has access to the request and response objects. Objects and variables which are created inside the service or doGet or doPost, etc method are only visible to the Thread handing that request.

It is the programmer's job to ensure that objects and variables which are potentially reachable by more than one request Thread are handled properly.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic