Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a synchronized object method in scala

Does scala support synchronized object (/static) methods? I am looking for:

synchronized def myObjectMethod(): <SomeReturnType> = {
.. 
 }

If this were not supported, what is the equivalent in scala ?

like image 464
WestCoastProjects Avatar asked Mar 06 '15 03:03

WestCoastProjects


1 Answers

synchronized in scala is just a method1. So you can do

def myObjectMethod: SomeReturnType = synchronized {
  // stuff
}

It's actually a special method injected by the compiler, more details here: How is the synchronized method on AnyRef implemented?

like image 160
Gabriele Petronella Avatar answered Oct 04 '22 08:10

Gabriele Petronella