2

I know there are many questions asked on this but in my case i have it in a synchronised block and i am sure that nobody will be changing my array list.. But still i get this exception SOMETIMES if i am using for-each loop.. Why ?

Here is my code snippet

    final static Object mLock = new Object();
private static ArrayList<ConnectionAndAuthCallback> mCallbacks;
private void callAuthCallbacks() {
        synchronized (mLock) {

            if (mCallbacks != null)
                for (ConnectionAndAuthCallback calback : mCallbacks) { //here i get exception
                    calback.onAuthentication(mToken, calback.intent);
                }
        }
    }

here is the code i do on Callback

@Override
            public void onAuthentication(String token, Intent intent) {
                web.loadUrl("xyz.com");
                //unregister so that we wong get any exception or some more callbacks
                SameClass.unRegisterAuthCallbacks(this); 
            }

and in Unregister function

public static void unRegisterAuthCallbacks(ConnectionAndAuthCallback callback) {
        synchronized (mLock) {

            if (mCallbacks != null)
                if (mCallbacks.contains(callback)) {
                    mCallbacks.remove(callback); // This causing problem ?? In same thread "syncronised" wont work ??
                }
        }
    }
14
  • 1
    Seems you are on wrong track. This code block should not cause this exception. Commented Jun 16, 2014 at 5:28
  • Where else in your code do you use mCallbacks? Are you iterating through mCallbacks at some point prior to the call to callAuthCallbacks()? Commented Jun 16, 2014 at 5:29
  • You could always create your own ArrayList subclass that logs whenever the list is changed and by whom, and substitute that in. That's always a fun debugging technique. Commented Jun 16, 2014 at 5:31
  • Yes, mCallbacks gets iterated through in some other places too but they are too inside sync block Commented Jun 16, 2014 at 5:32
  • Well, that's strange. Doesn't seem like the exception should be happening, and if none of us can reproduce your problem, helping you will be somewhat difficult... Try the ArrayList subclass thing and see if that helps? Commented Jun 16, 2014 at 5:35

2 Answers 2

1

You can't perform modifications on an ArrayList or remove() in this case that you are currently reading out of or looping through. Use iterators instead.

Check out this link. And this link.

Sign up to request clarification or add additional context in comments.

1 Comment

I had gone through this before i asked this question. And obiously it solves the problem.
0

Answer was, In the same thread synchronised(){} wont work. Even if you think it works then there might be a dead lock.

In my case i was removing callback object from ArrayList in the same thread where i called callback.

Comments

Your Answer

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.