scala object 线程安全
这篇Scala thread-safety question回复
提到如下代码:
1 | object Class { |
“object” initialization is thread safe. “mutable.List” is not thread
safe and needs to be synchronized.
然后我们自己定义一个object用于测试:
1 | object TestObject { |
用以下代码测试:
1 | object TestObjectInitializer extends App { |
打印如下代码:
1 | propinitialize |
propinitialize只执行一次。
参考资料:
Scala Singleton Object with Multi-threading
How to make object (a mutable stack) thread-safe