This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which variables r thread-safe

 
kesava chaitanya
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Public class TestServlet extends HttpServlet implements SingleThreadModel {
private int x;
public void doPost(HttpRequest req, HttpResponse res){
doPost () .
}

}
In above example instance variable x and request are both thread-safe or else only instance variable is thread safe.
and my next question is
what about class variables,localvariables,context attributes r also thread safe when u implement singlethreadmodel Interface
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is more of a Servlets than a JSP question, so moving this there.
 
Ram Prasadh Anandaraj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as your example is concerned. I think variable x is thread safe coz
you have implemented SingleThreadModel interface, which means that at any point in time, only one thread can access the service method. Since doPost is invoked by the service method, doPost and the methods that are invoked from within the doPost method are also threadsafe. The instance variable x should also be threadsafe, provided it is being accessed/modified by threadsafe methods.
Hope this helps.
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SingleThreadModel merely ensures that the Servlet instance is only accessed by a single thread at any time. However, this doesn't stop the Application Server from pooling multiple instances of the Servlet to service multiple concurrent clients... and in fact that is exactly what they do. This of course means that any resource that is shared among Servlets is no longer thread-safe... including the HttpSession and ServletContext. Thus implementing SingleThreadModel merely gives a false sense of security regarding threading issues. For this reason the Servlet 2.4 Expert Group strongly considering deprecating the SingleThreadModel for the latest version of the Servlet Spec... unfortunately it never quite made it in.
I strongly suggest that everyone do themselves a favor and NEVER implement SingleThreadModel. Learn how to write proper thread-safe code... afterall it is not that difficult. Your Applications and your career will thank you for it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic