aspose file tools*
The moose likes Java in General and the fly likes static methods multiple calls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "static methods multiple calls" Watch "static methods multiple calls" New topic
Author

static methods multiple calls

Suma Rangaraj
Ranch Hand

Joined: Feb 02, 2005
Posts: 50

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

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

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

Joined: Feb 02, 2005
Posts: 50


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

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Feb 02, 2005
Posts: 50

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

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static methods multiple calls