Quantcast
Channel: Singleton methods thread safe - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by Jon Skeet for Singleton methods thread safe

$
0
0

We have no idea whether it's safe or not - we don't know what someField.method() does.

I would strongly encourage you to make someField a final field, as if the singleton needs to mutate state then it's definitely not thread-safe without extra synchronization. If SomeClass itself is immutable and thread-safe, then you shouldn't need any other synchronization - but otherwise, you will.

Basically, there's nothing "magically thread-safe" about a singleton. It's just a single instance which multiple threads will have access to via the static getInstance() method. If the class is thread-safe, it's thread-safe regardless of whether it's a singleton - and if it's not thread-safe, then making it a singleton won't do anything to help that.


Viewing all articles
Browse latest Browse all 4

Trending Articles