Поиск…


Синтаксис

  • objectToSynchronizeOn.synchronized { /* code to run */}
  • synchronized {/* code to run, can be suspended with wait */}

синхронизировать объект

synchronized - это низкоуровневая конструкция параллелизма, которая может помочь предотвратить доступ нескольких потоков к тем же ресурсам. Введение для JVM с использованием языка Java .

anInstance.synchronized {
  // code to run when the intristic lock on `anInstance` is acquired
  // other thread cannot enter concurrently unless `wait` is called on `anInstance` to suspend
  // other threads can continue of the execution of this thread if they `notify` or `notifyAll` `anInstance`'s lock
}

В случае object s он может синхронизироваться с классом объекта, а не с экземпляром singleton.

синхронизировать неявно с этим

 /* within a class, def, trait or object, but not a constructor */
 synchronized {
   /* code to run when an intrisctic lock on `this` is acquired */
   /* no other thread can get the this lock unless execution is suspended with
    * `wait` on `this`
    */
 }


Modified text is an extract of the original Stack Overflow Documentation
Лицензировано согласно CC BY-SA 3.0
Не связан с Stack Overflow