R P
R P

Reputation: 45

W.r.t java threads, would there be any difference in time taken to process a synchronized static method vs non static method?

Consider two classes:

Class A {

   public static synchronized void printA() {
    ...//....
   }
}

Class B {

   public synchronized void printB() {
   ...//...
   }
}

Now, suppose I have 1 million threads fired on both printA and printB each. Will printA execution for 1 million threads faster than printB or vice-versa? OR total execution time is not guaranteed.

Upvotes: 1

Views: 162

Answers (3)

Adrian Shum
Adrian Shum

Reputation: 40056

it depends.

  1. synchronizing a static method and an instance method is semantically meaning different thing: former one is synchronizing on the whole class, while the latter one synchronized on a specific instance. Therefore all threads is competing on same resources for printA(), while it may or may not for printB(), depending on how many B instance and how you decide which B instance to use etc. In this aspect, synchronizing on instance method will not be worse than synchronizing on static method

  2. Apart from resource contention, there is also factor of virtual table lookup when invoking instance method. Therefore, if you are having only 1 instance of B and everyone is invoking printfB() for that instance (which makes resource contention aspect the same), then calling static method will not be worse than calling instance method. (please note, it doesn't mean it will always be better, coz the VM has the flexibility to do misc optimization to avoid vtable lookup).


However I feel the question is a bit strange and rarely meaningful. It is just like asking which of instance method and static method is faster. Though there are answer on it but that is rarely meaningful: You rarely decide use of instance or static method base on the performance issue. They are semantically different in meaning.

Upvotes: 0

yshavit
yshavit

Reputation: 43401

A major factor here is how many different objects you have. The static method will synchronize on the A.class object, meaning only one thread across your whole JVM will be able to be in printA() at a given moment (each class has exactly Class instance associated with it). The instance method will synchronize on the particular object, meaning many threads can call printB() at the same time, each working on its own B instance. Of course, if there is only one instance of B, then only one thread will be allowed to run printB() at a time.

The other major factor is your computer's physical resources! Spinning up 1 million threads doesn't magically make your CPU able to 1 million times as much work in the same unit of time. In fact, it'll probably slow down your program overall, as each thread is contending for time on your precious 4 or 8 cores (maybe more when you factor in hyperthreading or run it on a beefy server, but I'm betting you won't have anywhere near 1 million cores). Each time one thread wins out over a previously-running thread, it'll incur a context switch ("stop running that thread, start running this one"), which is expensive.

If your operations are very CPU-intensive, you won't get a better boost than the number of cores you have. If they all vie for a common resource (such as disk I/O), you probably won't get much better than just one or two threads, and again you could even see slower performance (if each thread causes the disk to seek to a different place each time it's scheduled for some work).

So basically, let's say you have 8 cores, 1 million threads and 20 different B objects spread evenly throughout those threads. With the static synchronized method, only one of them is allowed to work on printA() at a time. With the instance synchronized method, 20 different threads will be allowed to run printB() at a time (one on each of the 20 B instances), of which only 8 will physically be able to run at a time.

Upvotes: 0

HEKTO
HEKTO

Reputation: 4191

Static calls will work faster because your program won't need to locate them via an object. So, they will save you one referencing per call.

Upvotes: 1

Related Questions