Search code examples
javasynchronize

Java synchronize behaviour for static and non-static method


As per my understanding synchronize on non-static method will blocked at object level and synchronize on static method will be blocked at class instance level.

Based on that please find below my understanding on various scenarios:

  1. non-static synchronize method access then other non-static synchronize method also block

  2. non-static synchronize method access then non-static non-synchronize method doesn't block.

  3. static synchrnize method access then other synchronize (static & non-static) methods for that class instance blocked.

  4. static synchronize method access then other non-static non-synchronize method doesn't block.

  5. static synchronize method access then other static non-synchronize method doesn't block.

  6. static synchronize method access then non-static synchronize method blocked for all the object instance.

  7. non-static synchrnize method access then static synchronize method doesn't block

Please help me to verify the same.


Solution

  • I think it can be explained simpler

    1) when a thread has entered a synchronized instance method then no other thread can enter any of synchronized instance methods of the same instance

    2) when a thread entered a synchronized static method then no other thread can enter any of synchronized static methods of the same class