multithreading - Java threads holding a monitor -


i reading java threads , happened read this blog. in blog came across statement:

during execution of synchronized method, thread holds monitor method's object, or if method static, holds monitor method's class.

can please tell me difference between:

  • a thread holding monitor methods object
  • a thread holding monitor methods class

it means synchronized instance method broadly equivalent to:

public class fooclass {     public void foo() {         synchronized(this) {             ...         }     } } 

whereas synchronized static method broadly equivalent to:

public class fooclass {     public static void foo() {         synchronized(fooclass.class) {             ...         }     } } 

so in instance method case synchronize on instance of fooclass (and calls foo in other threads on other instances not block), whereas in static method case synchronizes on class object itself.

note static method version, it's class containing method important - if method notionally called "on" subclass (e.g. subfooclass.foo()) method still obtains monitor on class in it's declared.

see jls section 8.4.3.6 more details.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -