Why `this.synchronized` instead of just `synchroni

2019-04-18 09:16发布

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条回答
We Are One
2楼-- · 2019-04-18 09:57

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.

查看更多
登录 后发表回答