difference between synchronizing a static method and a non static method

后端 未结 7 1356
一向
一向 2020-12-02 11:27

What is the difference between synchronizing a static method and a non static method in java?Can anybody please explain with an example. Also is there any difference in sync

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 11:51

    There is virtually no difference between synchronizing a block and synchronizing a method. Basically:

    void synchronized m() {...}
    

    is the same as

    void m() { synchronized(this) {...} }
    

    By comparison a static synchronized method is the same as:

    static void m() { synchronized(MyClass.class) {...} }
    

提交回复
热议问题