【问题标题】:Is It a Good Idea to Make Singleton's getInstance() Method Asynchronous by Making It Returns an Observable<Singleton>?通过使其返回 Observable<Singleton> 来使 Singleton 的 getInstance() 方法异步是个好主意吗?
【发布时间】:2019-12-26 05:20:06
【问题描述】:

我有一个需要几秒钟来实例化的单例。它使 UI 冻结。所以我打算使getInstance() 方法异步。编写以下代码是一种常见的做法吗?

/*
 * The singleton class
 */
public class Singleton {

    private static volatile Singleton instance;

    public static Observable<Singleton> getInstance(Context context) {
        return Observable.fromCallable(() -> {
            synchronized (Singleton.class) {
                if (instance == null)
                    instance = new Singleton(context.getApplicationContext());
            }
            return instance;
        });
    }

    private Singleton(Context context) {
        // long running process
    }

    // ...
}



/*
 * The UI class
 */
public class UI extends Activity {

    private Singleton singleton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Singleton.getInstance(this)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(result -> {
                UI.this.singleton = result
            })
    }

    public void onButtonClick(View v) {
        if (singleton != null)
            singleton.someMethod();
    }
}

如果不是,为什么不是,更好的解决方案是什么?

【问题讨论】:

  • 没有。它不是。 Use an enum to implement singleton.
  • 不,相反,这完全没有意义。当你需要单例时,你需要它。引入异步机制然后等待其结果的任何意义都没有。
  • @user207421 如果没有异步,用户在几秒钟内无法与 UI 交互,因为 UI 冻结,这对 UX 不利。使用异步,用户可以在单例实例化的同时与 UI 交互。为什么没有意义?
  • @ElliottFrisch 并没有解决实例需要一段时间才能实例化的问题。当线程实例化单例时,UI 仍然冻结
  • @user207421 你读过编辑代码上的 UI 类吗?我清楚地从onCreate 方法异步调用getInstance 方法,然后在用户触摸按钮时使用创建的实例

标签: java android design-patterns rx-java rx-java2


【解决方案1】:

您想要的是缓存从可调用对象返回的值,因此下次调用订阅时您不想再次执行可调用对象。为了那个原因 使用cache 运算符。

Single<Integer> cachedValue = Single.fromCallable(() -> {
    Thread.sleep(3000);
    return 5;
}).cache();


cachedValue.subscribe(e -> System.out.println(System.currentTimeMillis() + ": " + e));
cachedValue.subscribe(e -> System.out.println(System.currentTimeMillis() + ": " + e));

您会注意到第二次通话的时间与第一次通话的时间太接近了。至少

【讨论】:

    【解决方案2】:

    检查此Safe Singleton。枚举是使其成为安全单例的方法

    【讨论】:

    • 没有解决实例实例化需要一段时间的问题。当线程实例化单例时,UI 仍然冻结
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多