• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is static calls within Servlet performant  RSS feed

 
Sudhakar Duraiswamy
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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 ?


 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • 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.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
K Abhijit
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!