12

I'm trying to move everything in my app away from singletons, because I've been made aware that it's a bad programming practice, with that said, I'm looking into implementing Dagger 2 dependency injection. And I'm wondering, when you do @Singleton in Dagger 2 is that thread synchronized? if not how can I synchronize it, so I don't get any strange data anomalies from multiple threads touching the same things.

When I was creating singletons before I'd do something like this:

public class SomeSinglton {
    private static ClassName sInstance;

    private SomeSinglton () {
    }

    public static synchronized ClassName getInstance() {
        if (sInstance == null) {
            sInstance = new ClassName();
        }
        return sInstance;
    }

is the Dagger 2 @Singleton equivalent as far as being synchronized?

1
  • you sure that method needs to be sync, and not sIntance? Maybe i'm wrong, but based on my expirience i would say that sync is not in a right place, but if i'm wrong, i want to know why
    – Nik Myers
    Commented May 14, 2015 at 14:55

4 Answers 4

13

Yes, @Singletons in Dagger 2 are thread safe with double checked locking, same in Dagger 1. See ScopedProvider.

1

As Artem Zinnatullin mentioned in his answer - instance creation of @Singleton classes is thread safe in Dagger.

But if you are going to touch that singleton from different theads you must make it thread safe by yourself. Otherwise Dagger won't help you.

Usually, @Singleton annotation should mean for other developers that such class can be used from different threads.

5
  • Thank you. is that something I should implement in the Module or in the class the Module is generating?
    – AlexW.H.B.
    Commented May 14, 2015 at 15:10
  • This should be related directly to the class. I recommend you to read something about java concurrency and how to make thread safe code. Synchronize a whole class is usually a bad idea.
    – Ayzen
    Commented May 14, 2015 at 15:13
  • 1
    This is not true, @SIngletons are thread safe in Dagger 2, see my answer below. Commented Dec 24, 2015 at 12:17
  • Actually I meant not instance creation of a class, but using. Despite the fact that creation is thread safe, using that instance from several threads is still not thread safe. I'll modify my answer. Thanks.
    – Ayzen
    Commented Dec 25, 2015 at 18:40
  • Ah, got it, sure, no magic, object must be thread safe if you're sharing it to multiple threads. Commented Dec 30, 2015 at 14:14
-2

Loot at this site. There are different approaches to implement Singleton, among them ThreadSafeSingleton

1
  • 2
    This should be a comment. Other than the link, it provides no true answer to the question. Commented May 14, 2015 at 14:57
-3

There's nothing wrong with singletons. But this is a better implementation.

public class SomeSinglton {
    private static ClassName sInstance = new SomeSinglton();

    private SomeSinglton () {
    }

    public static ClassName getInstance() {
        return sInstance;
    }

There's an implicit synchronization when the static field sInstance is initalized.

8
  • I don't think this answers my question... I'm referring to dependency injection with the Dagger 2 library. And I've now read in too many places that singletons are bad... they are bad because they are very hard to Unite Test, and because they tightly couple components. Also I'm pretty sure your code in that example is not thread safe. It's not synchronized. I think that would cause a lot of issues in a multithreaded app.
    – AlexW.H.B.
    Commented May 14, 2015 at 15:07
  • I'll bet that library implemented it correctly. And I assure you that my impl is correct and better :) trust me.
    – ZhongYu
    Commented May 14, 2015 at 15:10
  • As to whether singletons are bad - have you personally got into any problem with your singletons? An opinion does not become more correct if its repeated a million times; unfortunately that happens a lot in our industry.
    – ZhongYu
    Commented May 14, 2015 at 15:11
  • Could you explain how your implementation is better? and Yes I have gotten into trouble with Singletons. I've used them in an android app. They have caused a lot of odd state issues, but they also are really hard to test. I've started to unite test my app and realized it's almost impossible with Singletons. I do think you are right though that Singletons aren't inherently bad, but I do think they promote lazy programming at least in my case that is true.
    – AlexW.H.B.
    Commented May 14, 2015 at 15:15
  • thank you for clarifying your implementation. That's good to know. I had no idea that there was an implied synchronization.
    – AlexW.H.B.
    Commented May 14, 2015 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.