Merge "ConnectivityThread: use lazy holder idiom"

This commit is contained in:
Hugo Benichi
2016-10-20 23:49:01 +00:00
committed by Android (Google) Code Review

View File

@@ -27,25 +27,30 @@ import android.os.Looper;
* @hide * @hide
*/ */
public final class ConnectivityThread extends HandlerThread { public final class ConnectivityThread extends HandlerThread {
private static ConnectivityThread sInstance;
// A class implementing the lazy holder idiom: the unique static instance
// of ConnectivityThread is instantiated in a thread-safe way (guaranteed by
// the language specs) the first time that Singleton is referenced in get()
// or getInstanceLooper().
private static class Singleton {
private static final ConnectivityThread INSTANCE = createInstance();
}
private ConnectivityThread() { private ConnectivityThread() {
super("ConnectivityThread"); super("ConnectivityThread");
} }
private static synchronized ConnectivityThread getInstance() { private static ConnectivityThread createInstance() {
if (sInstance == null) { ConnectivityThread t = new ConnectivityThread();
sInstance = new ConnectivityThread(); t.start();
sInstance.start(); return t;
}
return sInstance;
} }
public static ConnectivityThread get() { public static ConnectivityThread get() {
return getInstance(); return Singleton.INSTANCE;
} }
public static Looper getInstanceLooper() { public static Looper getInstanceLooper() {
return getInstance().getLooper(); return Singleton.INSTANCE.getLooper();
} }
} }