java - Static synchronized methods and non-synchronized static methods confusion -


i have small confusion. please have @ code below.

public class threaddemo {   //non-static synchronized method   synchronized void a(){    actbusy();   }    //static synchronized method   static synchronized void b(){     actbusy();   }    //static method   static void actbusy(){     try{       thread.sleep(1000);     }     catch(interruptedexception e) {       e.printstacktrace();     }   }    public static void main(string[] args){     final threaddemo x = new threaddemo();     final threaddemo y = new threaddemo();     runnable runnable = new runnable() {       public void run() {          int option = (int) (math.random() * 4);          switch (option){            case 0: x.a();              break;            case 1: x.b();              break;            case 2: y.b();              break;            case 3: y.b();              break;          }       }     }   ;     thread t1 = new thread(runnable);     thread t2 = new thread(runnable);     t1.start();     t2.start();   } } 

i know sure invocation of sequence possible.

x.a() //in thread-1 y.b() //in thread-2 

though still have small confusion that, can see x.a() calls actbusy() method static method. method b() static synchronized method calling non-synchronized static method. when thread-2 gets class level lock, why call of actbusy() thread-1 not blocked?

i logically confused, if thread gets class level lock, class other non-synchronized static methods remain open called other methods (instance method). why?

actbusy() not synchronized callers methods are.

so thread 1 not block acquires lock on this object , no other thread holds lock on this, able call without problem.

that because non-static synchronized method locks on this present instance , not on class object.

x.a() grabs lock on present instance i.e x , no other thread able enter method a() of x until present present thread releases lock.

thread 1 --> x.a() //acquires lock , holds it

thread 2 ---> x.a() //blocks here until thread 1 releases lock on x

edit:

class object != instance  

so according jmm different objects , 2 threads don't interfere each other. allows call it.

edit 2:

why allow calls other static methods? logic behind it?

suppose this:

public static synchronized int statefulmethod(){     //this should protected }  public static int nonstatefulmethod(){     //just returns static value such 5     //so thread safe not have state }  public static synchronized int otherstatefulmethod(){     //this should thread safe } 

so if thread 1 in method statefulmethod() having shared state protect uses class level lock. thread 2 calls nonstatefulmethod() should not logically block method thread safe , there no point in making thread block here.

now if thread 3 calls otherstatefulmethod() while thread 1 holding class lock thread 3 have wait method static-synchornized.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -