1

I outputted the same object at the same time but I got different results... What could be the cause of DIFFERENT result?

The function in UserHelper.class:

public void login(String phone, String password) {
 UserModel.logInInBackground(phone, password, new LogInCallback<UserModel>() {
 @Override
 public void done(UserModel userModel, AVException e) {
 if (null != userModel) {
 if (userModel.getPosition() == UserModel.USER_BUYER) {
 refresh();
 DebugLog.e("fuck" + mUserStatus + UserInstance.getInstance().getUserStatus());
 for (UserListener listener : listeners)
 listener.OnUserLogin();
 } else if (userModel.getPosition() == UserModel.USER_SELLER)
 logout();
 } else for (UserListener listener : listeners)
 listener.HandleError(e.getCode());
 }
 }, UserModel.class);
public USER_STATUS getUserStatus() {
 return mUserStatus;
}

And the UserInstance.class.

public class UserInstance {
 public static UserHelper mInstance;
public static UserHelper getInstance() {
 if (null == mInstance) mInstance = new UserHelper();
 DebugLog.e(mInstance.toString());
 return mInstance;
 }
}
asked Mar 9, 2016 at 0:27
8
  • Which objects exactly should be the same? mUserStatus and UserInstance.getInstance().getUserStatus()? And which class contains the login method? Commented Mar 9, 2016 at 0:34
  • yes public USER_STATUS getUserStatus() { return mUserStatus; } Commented Mar 9, 2016 at 0:35
  • so login(..) is a method of the UserHelper?! Did you debug to the line, in which you logged the objects, to compare the object ids? I would need more code - where do you call the login-method? Commented Mar 9, 2016 at 0:41
  • Does your UserModel.logInInBackground() runs in UI thread or separate thread ? Commented Mar 9, 2016 at 0:54
  • In the first Activity when user click login button I call the login-method. And I logged the objects it do have different ids but I do not know why... Commented Mar 9, 2016 at 1:17

2 Answers 2

1

First of all, if you meant the UserHelper class to be a singleton, why do you access the USER_STATUS instance using UserInstance.getInstance().getUserStatus() instead of just getUserStatus() ?

Second of all, you probably get different instances of UserHelper if the singleton is accessed from different threads, because your implementation is not thread-safe.

A correct implementation would be using a double locking pattern:

public class UserInstance {
 public static UserHelper mInstance;
 private static final ReentrantLock lock = new ReentrantLock();
 public static UserHelper getInstance() {
 if (null == mInstance){
 lock.lock();
 try{
 if (null == mInstance){
 mInstance = new UserHelper();
 }
 }
 finally{
 lock.unlock();
 }
 }
 DebugLog.e(mInstance.toString());
 return mInstance;
 }

}

answered Mar 9, 2016 at 12:35
Sign up to request clarification or add additional context in comments.

Comments

0

Ultimately, I get the same instance..

Thanks to Shlomi Uziei. I forgot to use double locking pattern. And I should not make mInstance static...

answered Mar 10, 2016 at 5:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.