• 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

Is static calls within Servlet performant

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
We know that servlet container spawns thread for each request made (and it uses the same servlet instance).

My question is if all the that’s encompassed in the doService method are executed in a thread environment , shouldn’t all the method calls be instance method calls rather than static calls.

As we know thread acquires lock on that instance when an instance call is made /acquires call on the entire class if it is a static call.

So using static calls within the servlet would impact the performance as it would not release the lock for other threads until it finishes processing.


Is my understanding correct ?


 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudhakar,

In normal case when we call servlet separate thread is being started but it will not acquire lock for that thread. that why even if we are having static method in servlet it will not lock class.
Class level variable is not threadsafe in servlet.

for making that threadsafe you need to implement our servlet from SingleThreadModel interface.

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shailesh Narkhede wrote:for making that threadsafe you need to implement our servlet from SingleThreadModel interface.


No. The SingleThreadModel is deprecated. Do not use.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless the methods are synchronized, I can't see that it makes a difference.

If they are synchronized: since there's only one instance it also makes no difference, unless I'm missing something, since all threads are locking the same object regardless of whether it's an instance or static method.
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If they are synchronized: since there's only one instance it also makes no difference, unless I'm missing something, since all threads are locking the same object regardless of whether it's an instance or static method.



No, Static synchronized method will lock class, it is a class level lock for static synchronized method. all object of that class is get locked for executing the static synchrized method.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shailesh Narkhede wrote:

If they are synchronized: since there's only one instance it also makes no difference, unless I'm missing something, since all threads are locking the same object regardless of whether it's an instance or static method.


No, Static synchronized method will lock class, it is a class level lock for static synchronized method. all object of that class is get locked for executing the static synchrized method.


Yes I know. If it's static synchronized, all threads will lock on the Class object. If it's a synchronized instance method, all threads will lock on the single instance of the servlet. Therefore the threads all lock on the same object as each other, whether that is the class or the servlet.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as per Servlet spec 2.1, Container MAY create Multiple Servlet instances to address requests flows .... (need to check for 2.2 though )
This would change dynamics for few scenarios
 
reply
    Bookmark Topic Watch Topic
  • New Topic