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

Commit

Permalink
Merge pull request #3 from facebook/fix_singleton_thread_safety
Browse files Browse the repository at this point in the history
Added threadsafe singleton implementation to CCManager and BandwidthSampler
  • Loading branch information
rrong committed Apr 1, 2015
2 parents 61ca1bd + b72139f commit 53edbdf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,17 @@ public class ConnectionClassManager {
static final long BANDWIDTH_LOWER_BOUND = 10;

// Singleton.
@Nullable
private static ConnectionClassManager sInstance;
private static class ConnectionClassManagerHolder {
public static final ConnectionClassManager instance = new ConnectionClassManager();
}

/**
* Retrieval method for the DownloadBandwidthManager singleton.
* @return The singleton instance of DownloadBandwidthManager.
*/
@Nonnull
public static ConnectionClassManager getInstance() {
if (sInstance == null) {
sInstance = new ConnectionClassManager();
}
return sInstance;
return ConnectionClassManagerHolder.instance;
}

// Force constructor to be private.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,19 @@ public class DeviceBandwidthSampler {

private long mLastTimeReading;

// Make BandwidthTimerManager a singleton.
@Nullable
private static DeviceBandwidthSampler sInstance;
// Singleton.
private static class DeviceBandwidthSamplerHolder {
public static final DeviceBandwidthSampler instance =
new DeviceBandwidthSampler(ConnectionClassManager.getInstance());
}

/**
* Retrieval method for the BandwidthTimerManager singleton.
* @return The singleton instance of BandwidthTimerManager.
* Retrieval method for the DeviceBandwidthSampler singleton.
* @return The singleton instance of DeviceBandwidthSampler.
*/
@Nonnull
public static DeviceBandwidthSampler getInstance() {
if (sInstance == null) {
sInstance = new DeviceBandwidthSampler(ConnectionClassManager.getInstance());
}
return sInstance;
return DeviceBandwidthSamplerHolder.instance;
}

private DeviceBandwidthSampler(
Expand Down

0 comments on commit 53edbdf

Please sign in to comment.