为什么在Scala中使用"this.synchronized"而不是"synchronized"? [英] Why `this.synchronized` instead of just `synchronized` in Scala?
本文介绍了为什么在Scala中使用"this.synchronized"而不是"synchronized"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Scala中使用JDBC的示例中,有以下代码:
In an example of working with JDBC in Scala, there is a following code:
this.synchronized {
if (!driverLoaded) loadDriver()
}
为什么this.synchronized
而不是synchronized
?
推荐答案
在scala中,synchronized
不是关键字,就像在Java中一样.
In scala synchronized
is not a keyword, as in java.
它实际上是 AnyRef
的成员,用于Java的Object
.
因此,为回答您的问题,您可以使用synchronized
或this.synchronized
,就像您可以使用toString
或this.toString
一样.
So to answer your question, you can either use synchronized
or this.synchronized
, just as you can do toString
or this.toString
.
这篇关于为什么在Scala中使用"this.synchronized"而不是"synchronized"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文