Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8389641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:47:01+00:00 2026-06-09T18:47:01+00:00

Why does a call to removeListener() in the following code throw a ConcurrentModificationException when

  • 0

Why does a call to removeListener() in the following code throw a ConcurrentModificationException when another thread is using the iterator in fireEvent()?

public class MyClass {

    private Set<Object> synchronizedListeners;

    public MyClass() {
        synchronizedListeners = Collections.synchronizedSet(
                new LinkedHashSet<Object>());
    }

    public void addListener(Object listener) {
        synchronizedListeners.add(listener);
    }

    public synchronized void removeListener(Object listener) {
        synchronizedListeners.remove(listener);
    }

    public void fireEvent() {
        synchronized (synchronizedListeners) {
            for (Object listener : synchronizedListeners) {
                // do something with listener
            }
        }
    }
}

From my understanding, since I’m using synchronized (synchronizedListeners) in fireEvent(), this should block any other thread that calls removeListener(), till the iteration in fireEvent() is complete at which point it should be safe to remove an element from this Set. But this doesn’t seem to be the case. What am I doing wrong?

Possibly related: Java synchronized block vs. Collections.synchronizedMap

Edit: It was pointed out that I was unnecessarily synchronizing the removeListener() method. So I tried this version:

public void removeListener(Object listener) {
    synchronizedListeners.remove(listener);
}

But still got the same error.

Edit 2: As assylias pointed out, the problem isn’t visible in the above code. I was calling removeListener() from inside the for loop in the synchronized (synchronizedListeners) block which was causing the error. The fix I ended up using in this case is to remove the listener from another thread:

public void removeListener(final Object listener) {
    new Thread() {
        @Override
        public void run() {
            synchronizedListeners.remove(listener);
        }
    }.start();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:47:02+00:00Added an answer on June 9, 2026 at 6:47 pm

    I can’t reproduce what you describe – the code at the bottom gives the output shown below – which shows that remove is called in the middle of the iteration, but does not complete until after the iteration because you use a synchronized collection. That’s the behaviour one would expect and no ConcurrentModificationException is thrown. Note that I have removed the synchronized keyword from the removeListener method as it is useless here.

    fire 100000
    remove 100000
    done fire 100000
    done remove 99999
    

    Conclusion: the problem is somewhere else. For example, if you have a subclass that overrides the fireEvent method, or if you construct the synchronized set not exactly as in the code you posted.

    public static void main(String[] args) {
        final MyClass mc = new MyClass();
        final Object o = new Object();
        mc.addListener(o);
        for (int i = 0; i < 99999; i++) {
            Object o1 = new Object();
            mc.addListener(o1);
        }
        Runnable remove = new Runnable() {
    
            @Override
            public void run() {
                mc.removeListener(o);
            }
        };
    
        new Thread(remove).start();
        mc.fireEvent();
    }
    
    public static class MyClass {
    
        protected Set<Object> synchronizedListeners = Collections.synchronizedSet(new LinkedHashSet<Object>());
    
        public void addListener(Object listener) {
            synchronizedListeners.add(listener);
        }
    
        public void removeListener(Object listener) {
            System.out.println("remove " + synchronizedListeners.size());
            synchronizedListeners.remove(listener);
            System.out.println("done remove " + synchronizedListeners.size());
        }
    
        public void fireEvent() {
            System.out.println("fire " + synchronizedListeners.size());
            synchronized (synchronizedListeners) {
                for (Object listener : synchronizedListeners) {
                    // do something with listener
                }
            }
            System.out.println("done fire "  + synchronizedListeners.size());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A sample class in C# Class Desing Handbook (pg 137) does not call the
I have method that does asynchronous call to web service. Something like that: public
How does one call Go code in C from threads that weren't created by
How does one call an @selector method with multiple arguments? I have the following
void func() {assert(0);} int main () {void func();} The above code does not call
Does asynchronous call always create a new thread? Example: If JavaScript is single threaded
Where in the Android source code does Android call onKeyDown() when it receives notification
The following code builds error free on msvc, but does not compile on g++.
Does a call of unique() on a list also resize the list if repeats
Why does Sun call their VM for CDC devices C Virtual Machine ? Isn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.