• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Is a Servlet thread-safe

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is a servlet thread safe.If yes,how.
If not,how to make a servlet thread safe.

When there are multiple requests for multiple servlets,how is this handled.

When there are multiple requests for a single servlet,how is this handled
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlets are not thread safe. If you want to make it Servlet as Thread safe, you can implement SingleThreadInterface which is a blank Interface there is no methods (this is not recomend method, because it could slow the performance of your page) or you can synchronize methods by using synchronized keyword. Anyway, this is quite basic question, try to use google in this case, you will find a lot of pages with a full story about this .
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The SingleThreadModel interface is deprecated and due for removal. The problem is that it causes a performance hit while only providing the illusion of thread safety.
Assume that multiple threads will be able to access your servlet at any time (except during initialisation). Therefore local variables are safe but instance variables are not. The Request and Response are (typically) thread safe but any other shared or global instance such as Contexts, sessions etc are not and must be treated with care.
 
p hasini
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I searched in googled.But I didnt get a clear idea.

SingleThreadModel interface is deprecated from which version.

How can we make a servlet thread-safe without using SingleThreadModel.

Which methods do we synchronize in the servlet class.

As far as I know,we need to declare local variables not instance variables in our class.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deprecated from Servlets 2.4 (see the link to the interface and read the API)
There is no simple answer to making a Servlet thread safe. Making sure you don't have any instance or mutable static variables is a start, local variables are a safe replacement.
The full answer is that it is no longer a Servlet based problem but a threading problem.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're at all serious about concurrency in Java -and in these days of multi-core CPUs, multiple CPUs per machine, and multi-threaded environments like servlet containers and application servers, who can afford not to be?- I'd advise to work through one of the eminent book on the subject - either "Java Threads" by Oaks/Wong or "Java Concurrency in Practice" by Brian Goetz et al.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't read Java Threads but should, I read Java Concurrency in Practice cover to cover. Recommended read.
 
p hasini
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To make things simple,can we synchronize service() method.
or should we synchronize only the doGet() and doPost() methods
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A servlet should not override the service method, so you can't really synchronize that. Note that synchronizing the entire doGet or doPost methods effectively destroys concurrency for this servlet; that may be OK if there are multiple servlets and the one in question is only a small part of the web app, but generally you don't want to do that.

It might be OK to synchronize small, appropriate sections of those methods.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd certainly look very, very carefully at the data you believe you need to synchronize, and see if it really needs to be synchronized per-request.
 
Greenhorn
Posts: 24
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to bump an old thread but I wanted to post a link to an excellent article I just read on Javaworld which would be most useful to others that find their way here.

Write thread-safe servlets
 
Ranch Hand
Posts: 171
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really that is a nice article to understand. Some times before I've had some thread sharing problem and I've solved using inner class to initialize variables that are needed. This stops thread sharing of instance variables and it's up to your requirement.
 
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While that article demonstrates the various approaches to thread safety, I don't particularly like the example. You wouldn't use synchronization for protecting an int, you'd use an AtomicInteger instead.
 
reply
    Bookmark Topic Watch Topic
  • New Topic