• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Servlet inheritance and performance  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my app I've got a lot of servlets where I have to repeat a lot of code all the time. So I decided I would write a parent class than all my servlets would extend it.

Here is the 3 main methods from my parent class:



I override the doIt method in my children classes. My question is:
Does this approach have any performance problem? Is it slower than repeat all the code in the servlet classes?
I know the container allocates a thread (or grabs one from the pool) for a new request. So all the requests have a unique stack and a unique thread. But isn't it a problem that they all call the same methods first? (doPost and handleExceptions from the parent)
I'm always confused about this kind of situation. I don't really know how it happens. There is one method from one class which is not synchronized. A tons of requests get it (it = the same unsynchronized instance method from the same instance of a class, or the same unsynchronized static method from the same class). How does it happen in detail? How can that much request execute the same (the only one) method concurrently?

Thanks in advance.
 
author & internet detective
Sheriff
Posts: 38569
659
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inheritance is not slower than repeated code. Repeated code is an anti-pattern and something you should seek to avoid. If a method is not synchronized, it doesn't matter how many callers call it at the same time.

 
Bob Hunt
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!

But how does it actually happen? How can that many callers call and execute it at the same time?
 
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How can that many callers call and execute it at the same time?



Briefly:
Because each request is operating in its own Thread with its own request and response objects and its own thread stack of local variables.

Bill
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!