Picasso.with() should be synchronized#520
Closed
Macarse wants to merge 1 commit into
Closed
Conversation
Collaborator
|
This should use double-checked locking. You're optimizing for the worst case instead of the best case. |
Contributor
Author
public static Picasso with(Context context) {
if (singleton == null) {
synchronized (Picasso.class) {
if (singleton == null) {
singleton = new Builder(context).build();
}
}
}
return singleton;
} |
Collaborator
|
Yeah. That looks good! Then we skip the lock and the runtime branching strategy will favor the non-null case over time and the method call becomes almost free. |
Contributor
Author
|
@JakeWharton: Good point. I will create a second pull request with the fix. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I was using Picasso
2.2.0with OkHttp1.3.0and everything was working fine.I updated OkHttp to
1.5.4and I started seeing instances of square/okhttp#680After researching a bit I noticed that I had two Picasso instances in my app generated by a race condition in the method that generates the Picasso singleton. I am using Picasso from the main thread but also from a background service to prefetch images.
Apparently I had this bug before but, as @swankjesse explains in square/okhttp#650, the exception was masked by the fact that the cleanup callable was being run with a
Future.