How Synchronization works in Java?

后端 未结 8 579
广开言路
广开言路 2020-12-08 21:46

I have a doubt regarding Java Synchronization . I want to know if I have three Synchronized methods in my class and a thread acquires lock in one synchronized method other t

相关标签:
8条回答
  • 2020-12-08 22:27

    Yes, all threads but the one which acquired the lock will have to wait until the lock gets released again to be able to execute one of the three synchronized methods.

    Remember, a synchronized method is the same as a normal method surrounded by

    synchronized(this) {
        // method body
    } 
    
    0 讨论(0)
  • 2020-12-08 22:28

    If a class has 4 synchronize methods, then yes at a time only one thread will have access to those methods. I guess doubt here was, each thread can access diff synchronized methods at a time for single class instance. The answer is no. Only one thread can access synchronized methods at a time.

    0 讨论(0)
提交回复
热议问题