Concurrency in Java: synchronized static methods

前端 未结 6 1049
礼貌的吻别
礼貌的吻别 2020-12-09 21:05

I want to understand how locking is done on static methods in Java.

let\'s say I have the following class:

class Foo {
    private static int bar = 0         


        
6条回答
  •  死守一世寂寞
    2020-12-09 21:43

    These two calls do not synchronize in respect to each other. It is as you said, a caller of f.get() acquires the lock of f object and caller of Foo.inc() acquires Foo.class object's one. So the synchronization rules are the same as if instead of static call you called an instance synchronized method with another object.

提交回复
热议问题