This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes SingleThreadModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "SingleThreadModel " Watch "SingleThreadModel " New topic


Veda Dhawad

Joined: Jan 16, 2004
Posts: 4
In the Servlet 2.4 specification SingleThreadModel has been deprecated, why?
Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 226

This article has summed it up nicely:
It was generally agreed that SingleThreadModel was a false friend that allowed a servlet developer to believe that they did not need to be concerned about synchronization and threading. Unfortunately this mechanism only makes this true for instance variables of the servlet itself and does not offer any protection for access to sessions, datasources or any other shared resources. As passing parameters between methods via instance variables is hardly best OO practise, there is no real benefit from this interface. However it does result in some significant additional implementation complications for containers.

jyothi godavarthy
Ranch Hand

Joined: Jan 28, 2004
Posts: 135
Then whats the best alternative to make the instance variables thread safe.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12761
If you absolutely have to use instance variables, then use synchronized methods or blocks. Writing servlets requires a considerable mental shift from writing single-user, single-Thread applications. I think SingleThreadModel was used because people just had not managed to make that mental shift.
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
The best way to make your servlets threadsafe is to keep them reentrant by not using any instance variables. If you need to save data for use between requests and your servlet is an HttpServlet, you can save it in the HttpSession.

kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
It is sorta covered in the JavaRanch Style Guide.
subject: SingleThreadModel
Similar Threads
what is SingleThreadModel?
A question about 'single-threaded mode'
Starting servlets
Single Threaded Model Question
Servelet instances created in web.xml