为了说明@harp seal pup 的答案,这里有一个带有输出的示例演示,
我们可以观察到可变变量myname
受到每个正在运行的线程的影响。
但是val thname
对于每个线程,不可变对象不会受到影响或锁定。
object ObjectMultiThreadingTest {
def main(args: Array[String]): Unit = {
for (x <- 1 to 5) {
var thread = new MyThread()
thread.setName("Thread number :" + x.toString)
thread.start()
}
}
class MyThread extends Thread {
override def run(): Unit = {
Singleton.myname = this.getName
Singleton.call(this.getName)
}
}
object Singleton {
var myname = "Singleton"
def call(threadName: String): Unit = {
val thName = threadName
val random = new Random
for (i <- 1 to 5) {
println("My Name is set by " + myname)
val sleeptime = random.nextInt(1000)
println(
"But, I am now running " + thName + ", sleeping for " + sleeptime
)
Thread.sleep(sleeptime)
}
}
}
}
输出:
My Name is set by Thread number :5
My Name is set by Thread number :5
My Name is set by Thread number :5
But, I am now running Thread number :3, sleeping for 498
My Name is set by Thread number :5
My Name is set by Thread number :5
But, I am now running Thread number :5, sleeping for 968
But, I am now running Thread number :4, sleeping for 232
But, I am now running Thread number :2, sleeping for 562
But, I am now running Thread number :1, sleeping for 204
My Name is set by Thread number :5
But, I am now running Thread number :1, sleeping for 315
My Name is set by Thread number :5
But, I am now running Thread number :4, sleeping for 100
My Name is set by Thread number :5
But, I am now running Thread number :4, sleeping for 749
My Name is set by Thread number :5
But, I am now running Thread number :3, sleeping for 66
My Name is set by Thread number :5
But, I am now running Thread number :1, sleeping for 4
My Name is set by Thread number :5
But, I am now running Thread number :1, sleeping for 332
My Name is set by Thread number :5
But, I am now running Thread number :2, sleeping for 899
My Name is set by Thread number :5
But, I am now running Thread number :3, sleeping for 679
My Name is set by Thread number :5
But, I am now running Thread number :1, sleeping for 849
My Name is set by Thread number :5
But, I am now running Thread number :5, sleeping for 157
My Name is set by Thread number :5
But, I am now running Thread number :4, sleeping for 694
My Name is set by Thread number :5
But, I am now running Thread number :5, sleeping for 864
My Name is set by Thread number :5
But, I am now running Thread number :3, sleeping for 591
My Name is set by Thread number :5
But, I am now running Thread number :2, sleeping for 702
My Name is set by Thread number :5
But, I am now running Thread number :4, sleeping for 339
My Name is set by Thread number :5
But, I am now running Thread number :3, sleeping for 959
My Name is set by Thread number :5
But, I am now running Thread number :5, sleeping for 443
My Name is set by Thread number :5
But, I am now running Thread number :2, sleeping for 522
My Name is set by Thread number :5
But, I am now running Thread number :5, sleeping for 368
My Name is set by Thread number :5
But, I am now running Thread number :2, sleeping for 851