开发者

synchronized blocks for static and non-static methods

开发者 https://www.devze.com 2023-01-29 19:46 出处:网络
I created two threads and using a single instance of class called the static and non-static methods of that object. Ideally static methods need to be called using the class name and i did that too.

I created two threads and using a single instance of class called the static and non-static methods of that object. Ideally static methods need to be called using the class name and i did that too.

I synchronized both the static and non-static methods on a private static member of the class whose methods the threads are calling. I noticed that the output was synchronized!

My questions are:

  1. Static methods if synchronized using a synchronized block it usually requires the class instance, then how did it accept a static object!

  2. How was the output synchronized as threads calling static methods acquire the class level lock and threads calling non-static methods acquire object level lock!

    Even though i have used synchronized block in both the static and non-static methods based on an object, it really shouldn't synchronize; or is it special case with synchronized blocks on static objects?

Please let me know.

Following is the code i have written:

public class StaticNonStaticSynch 
{
 public static void main(String[] args) 
 {
  final StaticNonStaticTest staticNonStaticTest = new StaticNonStaticTest();

  Runnable runnable1 = new Runnable() 
  {
   @Override
   public void run() 
   {
    staticNonStaticTest.nonStaticMethod();
   }
  };

  Runnable runnable2 = new Runnable() 
  {
   @Override
   public void run() 
   {
    staticNonStaticTest.staticMethod();
   }
  };

  Thread thread1 = new Thread(runnable1, "First Thread");
  Thread thread2 = new Thread(runnable2, "Second Thre开发者_高级运维ad");

  thread1.start();
  thread2.start();
 }
}

class StaticNonStaticTest
{
 private static Object object = new Object(); 

 void nonStaticMethod()
 {
  synchronized (object) 
  {
   for(int i=0;i<500;i++)
   {
    System.out.println("Non - Static method called by " + Thread.currentThread().getName() +" : = "+i);
   }
  }
 }

 static void staticMethod()
 {
  synchronized (object)
  {
   for(int i=0;i<500;i++)
   {
    System.out.println("Static method called by " + Thread.currentThread().getName() +" : = "+i);
   }
  }
 }
}


Globally, there exists one instance of the thing called StaticNonStaticTest.object. Whenever you synchronize on that thing (regardless where from), you are synchronizing on the same lock.


You are synchronizing on the basis of a static object,that's the reason the lock is obtained at the class level and both the static and non-static method's are synchronized. If you comment out the line "synchronized (object)" in the static and non static method's you can see that the calls are no longer synchronized.

0

精彩评论

暂无评论...
验证码 换一张
取 消