search

Home  >  Q&A  >  body text

java - synchronized同步问题

一个类中有两个同步方法,此时有多个线程同时访问该对象的两个方法,这样会造成锁互斥吗?

PHP中文网PHP中文网2802 days ago687

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:52:07

    Yes, it is this object that is locked, not a method.

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:52:07

    Yes, if it is a static method, the class is locked, that is, all instances of the class can only be accessed by one person. If it is a normal method, the object is locked.

    reply
    0
  • PHPz

    PHPz2017-04-18 10:52:07

    The synchronized keyword locks the object, that is, the object lock. The static method can be considered to obtain the lock of the corresponding Class object. When multiple threads compete for the lock of an object, only one will get the lock.

    reply
    0
  • Cancelreply