Recherche…


Syntaxe

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

synchroniser sur un objet

synchronized est une construction de concurrence de bas niveau qui peut empêcher plusieurs threads d'accéder aux mêmes ressources. Introduction pour la JVM utilisant le langage 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
}

En cas d' object il peut se synchroniser sur la classe de l'objet, pas sur l'instance singleton.

synchroniser implicitement sur ce

 /* 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
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow