• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

SingleThreadModel

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got a question in JWebPlus witch states that servlets implementing SingleThreadModel are ThreadSafe. Ok, there is gonna be only on thread at time executing service method, what about Session Attributes, ServletContext Attributes and Classes Variables?
Thanks,
Aleks
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. SingleThreadModel does little to help avoid threading issues outside the servlet itself.
Anyone who claims that SingleThreadModel is anything other than a hack to help with some sorts of legacy code access problems is overstating its benefits.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I got a question in JWebPlus witch states that servlets implementing SingleThreadModel are ThreadSafe. Ok, there is gonna be only one thread at time executing service method, what about Session Attributes, ServletContext Attributes and Classes Variables?


For obvious reasons, the session and servlet context cannot be made threadsafe automatically. The only way, that would have been possible is if all invocations have their own copies of session and servlet context, which is simply not possible.
Synchronizing access to resources shared between servlets simply cannot be done by any container and will always remain the responsibility of the developer.
Implementing SingleThreadModel will not even prevent static variables from being modified by several servlets at the same time.
Shashank
 
Enthuware Software Support
Posts: 4907
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aleks,
There was a similar doubt posted earlier also. I don't think it is mentioned anywhere in JWebPlus that a Servlet is thread safe if it implements STM. Can you please tell me the question id where it says that?
thanks,
Paul.
 
Aleks Pascoal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody, but anyone can say yes or no for sure to my question?
Aleks
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say yes because the servlet instance itself is infact thread safe.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is only one servlet instance per registered servlet. Normally, when concurrent requests are handled by the web server, threads are spawned which executes the service() method of the servlet. Instance variables are not thread safe, but local variables within the service() method are. The request object is also "localized" per thread.
According to the Servlet specs, when a servlet implements the SingleThreadModel interface (which is an empty "tag" interface) and concurrent requests are handled by the web server, the container is supposed to create a new servlet instance per request. It is possible that the container will pool these servlet instances. it is also possible that the container will serialize, i.e. queue, the requests per instance. It is possible that the container implementation is a combination of these.
Since there are now multiple servlet instances handling each request, the instance variables are now thread safe.
In either case, however, if any servlet static variables are defined, these will NOT be thread safe unless you explicitly make it so with custom code.
The same goes for any external resources your servlets access.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aleks
Thread safety means, shared resources are not accessed simulataneously, thereby avoiding data corruption. In the case of a thread safe servlet, a shared resource could be an instance variable.
And to avoid data corrpution you need to synchronize its access. This is the normal way
of providing thread safety. But it doesn't mean that to avoid concurrent access, you start creating new objects for every request.
However, if your servlet implements SingleThreadModel interface, it essentially does that - creates a new instance of servlet object for every request. This way the instace variables are safe but they are not shared, and do not serve any purpose for which threads are provided in the first place.
Consider a small example:
If you 've a counter which counts the number of times ur servlet has been accessed, then you will make it an instance variable and you would like it to be thread safe at the same time. Here the SingleThreadModel is of no use. 'coz every instance of ur servlet will have its own counter variable. In this case you have no choice but to synchronize the access to counter varible programatically.
Hence, SingleThreadModel does make you servlet thread safe as it creates a new instance for every request, but its useless as it doesn't serve the purpose for which you might want to make ur servlet thread safe in the first place.
 
Shashank Tanksali
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hence, SingleThreadModel does make you servlet thread safe as it creates a new instance for every request, but its useless as it doesn't serve the purpose for which you might want to make ur servlet thread safe in the first place.


My understanding is that a new instance is not created for every request. Rather the requests are all handled in sequence on the same instance.
Of course, the container might maintain several instances if there are lots of requests for a single servlet. However it still does not create a new instance for every request, which could be a really expensive operation and would adversely affect the performance.
Shashank
 
Tauqueer Ali
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shashank
"Rather the requests are all handled in sequence on the same instance."
This is exactly what happens in a simple servlet which doesn't implement SingleThreadModel. Only one instance of the servlet object is created. and instance variables persist across the requests.
In SingleThreadModel, as u mentioned it may be possible that the container uses the same instance of servlet instead of creating a new instance for every request. But this is called pooling in which the state of instance is reset.
this does not mean
"the requests are all handled in sequence on the same instance."
 
Shashank Tanksali
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"Rather the requests are all handled in sequence on the same instance."
This is exactly what happens in a simple servlet which doesn't implement SingleThreadModel.


I think that is not right.
When a servlet does not implement SingleThreadModel, the container handles the requests in parallel, not in sequence as you imply since by not implementing the SingleThreadModel, you are telling the container that your servlet is threadsafe and it is okay to have several invocations of the servlet in different threads at the same time.

Hence, SingleThreadModel does make you servlet thread safe as it creates a new instance for every request


What I was implying from my earlier post was that a new instance is not created for every request as you mention, but a currently free instance is reused.
Shashank
 
Tybon Wu
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My understanding is that a new instance is not created for every request. Rather the requests are all handled in sequence on the same instance.


I think it depends on the servlet container.
 
reply
    Bookmark Topic Watch Topic
  • New Topic