Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.

Commit 53edbdf

Browse files
committed
Merge pull request #3 from facebook/fix_singleton_thread_safety
Added threadsafe singleton implementation to CCManager and BandwidthSampler
2 parents 61ca1bd + b72139f commit 53edbdf

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

connectionclass/src/main/java/com/facebook/network/connectionclass/ConnectionClassManager.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,17 @@ public class ConnectionClassManager {
6464
static final long BANDWIDTH_LOWER_BOUND = 10;
6565

6666
// Singleton.
67-
@Nullable
68-
private static ConnectionClassManager sInstance;
67+
private static class ConnectionClassManagerHolder {
68+
public static final ConnectionClassManager instance = new ConnectionClassManager();
69+
}
6970

7071
/**
7172
* Retrieval method for the DownloadBandwidthManager singleton.
7273
* @return The singleton instance of DownloadBandwidthManager.
7374
*/
7475
@Nonnull
7576
public static ConnectionClassManager getInstance() {
76-
if (sInstance == null) {
77-
sInstance = new ConnectionClassManager();
78-
}
79-
return sInstance;
77+
return ConnectionClassManagerHolder.instance;
8078
}
8179

8280
// Force constructor to be private.

connectionclass/src/main/java/com/facebook/network/connectionclass/DeviceBandwidthSampler.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,19 @@ public class DeviceBandwidthSampler {
4141

4242
private long mLastTimeReading;
4343

44-
// Make BandwidthTimerManager a singleton.
45-
@Nullable
46-
private static DeviceBandwidthSampler sInstance;
44+
// Singleton.
45+
private static class DeviceBandwidthSamplerHolder {
46+
public static final DeviceBandwidthSampler instance =
47+
new DeviceBandwidthSampler(ConnectionClassManager.getInstance());
48+
}
4749

4850
/**
49-
* Retrieval method for the BandwidthTimerManager singleton.
50-
* @return The singleton instance of BandwidthTimerManager.
51+
* Retrieval method for the DeviceBandwidthSampler singleton.
52+
* @return The singleton instance of DeviceBandwidthSampler.
5153
*/
5254
@Nonnull
5355
public static DeviceBandwidthSampler getInstance() {
54-
if (sInstance == null) {
55-
sInstance = new DeviceBandwidthSampler(ConnectionClassManager.getInstance());
56-
}
57-
return sInstance;
56+
return DeviceBandwidthSamplerHolder.instance;
5857
}
5958

6059
private DeviceBandwidthSampler(

0 commit comments

Comments
 (0)