java - is @Singleton in dagger 2 thread safe? -
i'm trying move in app away singletons, because i've been made aware it's bad programming practice, said, i'm looking implementing dagger 2 dependency injection. , i'm wondering, when @singleton in dagger 2 thread synchronized? if not how can synchronize it, don't strange data anomalies multiple threads touching same things.
when creating singletons before i'd this:
public class somesinglton { private static classname sinstance; private somesinglton () { } public static synchronized classname getinstance() { if (sinstance == null) { sinstance = new classname(); } return sinstance; } is dagger 2 @singleton equivalent far being synchronized?
as artem zinnatullin mentioned in answer - instance creation of @singleton classes thread safe in dagger.
but if going touch singleton different theads must make thread safe yourself. otherwise dagger won't you.
usually, @singleton annotation should mean other developers such class can used different threads.
Comments
Post a Comment