What does 'synchronized' mean?

后端 未结 17 1663
广开言路
广开言路 2020-11-22 00:13

I have some questions regarding the usage and significance of the synchronized keyword.

  • What is the significance of the synchronized
17条回答
  •  执笔经年
    2020-11-22 00:22

    synchronized simple means no two threads can access the block/method simultaneously. When we say any block/method of a class is synchronized it means only one thread can access them at a time. Internally the thread which tries to access it first take a lock on that object and as long as this lock is not available no other thread can access any of the synchronized methods/blocks of that instance of the class.

    Note another thread can access a method of the same object which is not defined to be synchronized. A thread can release the lock by calling

    Object.wait()
    

提交回复
热议问题