difference between synchronizing a static method and a non static method

后端 未结 7 1355
一向
一向 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 12:09

    Synchronization in Java is basically an implementation of monitors. When synchronizing a non static method, the monitor belongs to the instance. When synchronizing on a static method, the monitor belongs to the class. Synchronizing a block of code is the same idea, but the monitor belongs to the specified object. If you can get away with it, synchronized blocks are preferable because they minimize the time each thread spends in the critical section

提交回复
热议问题