1

I have a static method

public static void abc(String str) { // some code str = str + "s"; // some code } 

Lets assume that this method is called by 100 threads at the same time.

I think that CPU schedules all these threads to execute this static method.

Lets scale up execute requests. Now, there are around 100000 threads calling this static method around same time.

If it is so, this will be a performance overhead (compared to the case where this method is a member of a class). Am I correct?

4
  • 3
    The cost of context switching the threads will far exceed any difference due to static vs non-static. Commented Nov 11, 2016 at 11:01
  • 1
    I have no idea what you might even be implying. The performance hit of what exactly? How is it different to call an instance method vs. static method? Commented Nov 11, 2016 at 11:04
  • @MarkoTopolnik There is only one instance of the static method of a class in memory. If all threads are invoking this static method around same time, there might be waiting time for remaining threads. Commented Nov 12, 2016 at 2:17
  • 1
    Ok, then your question comes from basic misunderstanding. There's only one instance of any method and any number of threads may execute it at any time. Only locks cause mutual exclusion. Commented Nov 12, 2016 at 5:57

2 Answers 2

5
  • In theory, a static call can be made slightly more efficient because it doesn't need to do a virtual function lookup, and it can also avoid the overhead of the hidden "this" parameter.
  • In practice, many compilers will optimize this out anyway.
  • The cost of context switching the threads will far exceed any difference due to static vs non-static method calling
Sign up to request clarification or add additional context in comments.

Comments

4

From the perspective of the OS's scheduler it doesn't matter whether the method is static or not. At machine level both are just subroutines with some parameters. So unless neither methods are synchronized, there shouldn't be any difference.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.