android_sdk icon indicating copy to clipboard operation
android_sdk copied to clipboard

removed locking from Adjust singleton creation

Open toby78 opened this issue 5 years ago • 2 comments

Removed locking from Adjust singleton creation. Create instance immediately when the class is loaded or initialized (Android ClassLoader). It will be 100% thread safe and the object instance will already be available before any thread accesses it. If class should not be created, disable the SDK instead.

toby78 avatar Dec 10 '19 10:12 toby78

While I agree that removing the synchronized removes some unnecessary locks, I'm not sure changing it to early initialization is worth it. What it could be changed to, is something like the double checked locking, making it looking to something like this:

    private volatile AdjustInstance defaultInstance;

    public static void AdjustInstance getDefaultInstance()
    {
        if (defaultInstance == null) {
            synchronized (Adjust.class) {
                if (defaultInstance == null) {
                    defaultInstance = new AdjustInstance();
                }
            }
        }
        return defaultInstance;
    }

nonelse avatar Dec 10 '19 11:12 nonelse

PR updated

toby78 avatar Dec 13 '19 07:12 toby78