The difference between synchronize and lock? --The difference between wait and sleep?

1. The difference between synchronize and lock

  1. synchronize is a java keyword, lock is a java class
  2. synchronize is suitable for locking a small part of code, and lock is suitable for locking a large amount of code
  3. synchronize will automatically release the lock, lock needs to be released manually, otherwise it will deadlock

Two, the difference between wait and sleep

  1. The scope is different, wait can only be used in synchronize, sleep can be used anywhere
  2. Different classes, wait comes from Object, sleep comes from Thread
  3. wait will release the lock, sleep will not release the lock

Guess you like

Origin blog.csdn.net/cst522445906/article/details/121250732