• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SingleThreadModel- when to choose that approach?

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that the SingleThreadModel-interface slows the performance, but isn't that to prefer anyway, for safety, if your application is not speed-dependent or number of users never is more than, say, 20?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The SingleThreadModel empty interface is implemented by servlets that does not wish to handle more than one event at a time. It is the servlet engines responsibility to ensure that it only ever invokes one callback method in the servlet at a time.

A server that loads a SingleThreadModel servlet must guarantee, according to the Servlet API documentation, "that no two threads will execute concurrently the service method of that servlet." To accomplish this, each thread uses a free servlet instance from the pool. Thus, any servlet implementing SingleThreadModel can be considered thread safe and isn't required to synchronize access to its instance variables.
For example, a servlet that connects to a database sometimes needs to perform several database commands atomically as part of a single transaction. Normally, this would require the servlet to synchronize around the database commands (lets say, it manage just one request at a time) or to manage a pool of database connections where it can "check out" and "check in" connections (say, it support multiple concurrent requests). By instead implementing SingleThreadModel and having one "connection" instance variable per servlet, a servlet can easily handle concurrent requests by letting its server manage the servlet instance pool (which doubles as a connection pool).
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's an acceptable performance concession today may not be tomorrow. Don't use single-threading just as a "easy way out"!
On a purely theoretical viewpoint, any need to limit myself to a single-thread approach implies that I wasn't clever enough to get the job done multi-threaded - though I'm sure that sooner or later in the real world, I'll find exceptional cases where single-threading actually BOOSTS performance or some other desirable property.
In the mean time, I limit my use of single-threading to cases where code is either being debugged and I want to eliminate one of the variables in the mix or a system has broken and single-threading gives me a "quick fix" so I can repair it without being under pressure.
Mostly this comes under the rule that it's easier to go from a general solution to a specific one than to back off of one specific solution and move to a different specific solution.
 
Men call me Jim. Women look past me to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic