Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static methods multiple calls

 
Suma Rangaraj
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi -

What happens when multiple instances call a class's static method? How does JVM handle multiple calls to that method? Will each instance has its own execution part or are the requests queued?

Thanks,
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends if the calls are made from different threads. If one thread then sequentially and with more it could run concurrently and in that case synchronization or something alike might be needed.
 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

This is a problem with Java allowing a static method to be accessible with a class instance.

Okay, to answer your question

1) Is your question with in a context of multiple threads ?.

This would cause havoc, unless you synchronize the static method.

2) Is your question with in a context of a single thread ?.

Not an issue.

HTH
-SM.
 
Suma Rangaraj
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi Wouter -

Even if multiple threads are accessing, we have only 1 method of static class per JVM right? How then can it be executed concurrently?


Thanks
 
Mike Simmons
Ranch Hand
Posts: 3078
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Easily. A method is just a set of instructions. If I give the same set of instructions to 100 people (or 100 threads), they're all following that same set of instructions, concurrently. The fact that they all their instructions are the same doesn't prevent them from executing those instructions at the same time. Not by default, anyway.

The problem is the data. Does the data being used change over time? Do the different people / threads change the data themselves? Are different people / threads changing the same data, simultaneously? Or is each person or thread using a different set of data? If they're using different data, that's no problem. But if they are sharing data, and changing it, you probably need to set up some rules to ensure that they don't interfere with each other too much. For Java threads, this usually means using synchronization, or java.util.concurrent locks. That's a longer discussion - but if you are using synchronization or locks, that means you are setting up something that can prevent two different threads from executing code to alter the same data at the same time. So, if you have done this, that may prevent two different threads from executing the same method at the same time. But if you haven't done this (hopefully, because there was no need to do this), then nothing prevents multiple threads executing the same method at the same time.
 
Suma Rangaraj
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for your explanation Mike...

If assuming my static function has no shared data between threads, what then is the advantage of using static method vs instance method?

 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that only one copy of each method is loaded. The state
of each object's method call (parameters and local variables) is saved on the
stack. The currently executing thread operates on its own state using the
shared method logic. Static methods are no different than instance methods.
They are just restricted to operating on static variables, those available to all
instances of the class.

Jim ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic