Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need to make static methods as synchronized if it is called within synchronized non static method?

Tags:

java

My doubt is do we need to make static methods as synchronized if it is called within synchonized non static method?

for e.g.

class Test
{

      public static void m2()
      {


      }

      public synchronized void m1()
      {

            Test.m2();
             ----
            ----
      }  

In above case do I need to make m2 as synchronized in order to avoid race condition or should I keep it as it is.

}

like image 460
Umesh K Avatar asked Dec 09 '10 07:12

Umesh K


People also ask

What will happen if the method on synchronized has to be called is a static method?

Static Synchronized method is also a method of synchronizing a method in java such that no two threads can act simultaneously static upon the synchronized method. The only difference is by using Static Synchronized. We are attaining a class-level lock such that only one thread will operate on the method.

Can we use static and synchronized together?

static methods can be synchronized. But you have one lock per class. when the java class is loaded coresponding java.

What is the difference between synchronized static method and synchronized non static method?

A synchronized block of code can only be executed by one thread at a time. Synchronization in Java is basically an implementation of monitors . When synchronizing a non static method, the monitor belongs to the instance. When synchronizing on a static method , the monitor belongs to the class.

What will happen if 2 threads call a static synchronized method of different objects?

When a thread calls a synchronized method, it acquires the intrinsic lock. After the thread finishes executing the method, it releases the lock, which allows other threads to acquire the lock and get access to the method.


2 Answers

It depends on what your static method is doing. Do you really need it to be synchronized at all? Is it accessing shared mutable state?

If so, you probably do need to synchronize (although I wouldn't do so just with the synchronized modifier - I'd create a private static final variable with an object to lock on.)

The fact that your instance method is synchronized means that no two threads will be executing it with the same target object - but two threads could both be executing m1 with different target objects, so m2 could be called twice at the same time. Whether that's a problem or not depends on what it's doing. If it's not using any shared state (e.g. it's really just computing something based on its parameters) then it doesn't need to be synchronized at all.

Generally speaking, it's more important for static methods to be thread-safe than instance methods: I typically don't make types themselves thread-safe, but instead try to use a few classes to manage concurrency, with each thread using its own set of separate objects as far as possible.

like image 169
Jon Skeet Avatar answered Sep 22 '22 12:09

Jon Skeet


You do need to make m2 synchronized. Otherwise someone can call that method at the same time. I am assuming m2 would be considered for being synchronized, otherwise it is a moot point.

like image 21
fastcodejava Avatar answered Sep 20 '22 12:09

fastcodejava