为什么我的单例是同步的,但它被创建了很多次?

Why my Singleton is synchronized but it gets created many times?

出于某种原因,我的单例存储库多次从后台线程创建,同步应该有所帮助。有人能帮忙吗?如果需要,我会提供代码片段和github link。

我的 IntentService class:

  @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.d(MainActivity.TAG, "ArticleIntentService - onHandleIntent");


        LiveData<List<Article>> liveArticles = ArticleRepository.getInstance(getApplication())
                .loadFromNetwork(PAGE_NUMBER, PAGE_SIZE);
        PAGE_NUMBER++;
        liveArticles.observeForever(articles -> {
            Log.d(MainActivity.TAG, "onStartJob - onChanged!!!!!!");
//                liveArticles.removeObserver(this);
            NotificationUtils.showNotification(context, articles.get(0).getSectionName(), articles.get(0).getWebTitle());

        });

    }

我的存储库:

public static ArticleRepository INSTANCE;

public static synchronized ArticleRepository getInstance(Application application){
    if(INSTANCE == null){
        Log.d(TAG, "ArticleRepository getInstance is NULL");
        return  new ArticleRepository(application);
    }

    return INSTANCE;
}

private ArticleRepository(Application application) {
    Log.d(TAG, "ArticleRepository constructor");
    mContext = application;
    mArticles = new MutableLiveData<>();
    ArticleRoomDatabase db = ArticleRoomDatabase.getInstance(application);
    mArticleDao = db.articleDao();
}

你从不赋值 INSTANCE:

public static synchronized ArticleRepository getInstance(Application application){
    if(INSTANCE == null){
        Log.d(TAG, "ArticleRepository getInstance is NULL");
        INSTANCE = new ArticleRepository(application);
    }

    return INSTANCE;
}