Why `this.synchronized` instead of just `synchronized` in Scala?

偶尔善良 提交于 2019-12-06 16:44:52

问题


In an example of working with JDBC in Scala, there is a following code:

this.synchronized {
  if (!driverLoaded) loadDriver()
}

Why this.synchronized instead of just synchronized?


回答1:


In scala synchronized is not a keyword, as in java.

It is in fact a member of AnyRef, which is scala equivalent for java's Object.

So to answer your question, you can either use synchronized or this.synchronized, just as you can do toString or this.toString.



来源:https://stackoverflow.com/questions/7826822/why-this-synchronized-instead-of-just-synchronized-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!