MOCKSTACKS
EN
Questions And Answers

More Tutorials









Scala synchronized

Examples

synchronize on an object


synchronized is a low-level concurrency construct that can help preventing multiple threads access the same resources. Introduction for the JVM using the Java language.

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
}

In case of objects it might synchronize on the class of the object, not on the singleton instance.

synchronize implicitly on this


 /* 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`
 */
 }



Conclusion

In this page (written and validated by ) you learned about Scala synchronized . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala Testing with ScalaCheck.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.