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

  • SEARCH
  • Home
  • 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 6183291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:20:23+00:00 2026-05-24T01:20:23+00:00

I have a HashMap of Sound objects private HashMap<Integer, Sound> sounds; over which I’m

  • 0

I have a HashMap of Sound objects

private HashMap<Integer, Sound> sounds;

over which I’m trying to iterate to turn off all the sounds. I used
this answer to create an Iterator, but I’m still getting ConcurrentModificationException, though I’m sure there’s no other code calling this at the same time.

public synchronized final void stopAll() {
    Iterator<Entry<Integer, Sound>> soundEntries = sounds.entrySet().iterator();
    while(soundEntries.hasNext())
    {
        Entry<Integer, Sound> s = soundEntries.next();
        s.getValue().myOnCompletionListener = null;
        s.getValue().fadeYourself();
    }
    sounds.clear();
}

In what way should I rewrite this to keep the ConcurrentModificationException from happening?

This is inside my Sound class:

    private class soundFader extends AsyncTask<Sound, Void, Void>
    {
        @Override
        protected Void doInBackground(Sound... arg0) {
            arg0[0].fadeOut();
            return null;
        }
    }

    private void fadeOut()
    {
        float STEP_DOWN = (float) 0.10;
        float currentVol = myVolume;
        float targetVol = 0;
        if(isSoundEnabled())
        {
            while(currentVol > targetVol)
            {
                currentVol -= STEP_DOWN;
                mp.setVolume(currentVol, currentVol);
                try {
                    Thread.sleep(70);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        mp.setVolume(0, 0);
        onCompletion(mp);
        sounds.remove(resource);    // THIS LINE WAS MY ERROR
        mp.seekTo(0);
        nowPlaying = false;
    }

    public void fadeYourself()
    {
        soundFader fader = new soundFader();
        fader.execute(this);
    }
  • 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-05-24T01:20:26+00:00Added an answer on May 24, 2026 at 1:20 am

    It is not permissible for one thread to modify a Collection while another thread is iterating over it.

    If you want to modify only values (not keys) there is no need to use iterators here.

    public synchronized final void stopAll() {
        for(Sound s: sounds.values())
        {
            s.myOnCompletionListener = null;
            s.fadeYourself();
        }
        sounds.clear();
    }
    

    Ninja edit:
    You are removing items from the Collection while iterating. Hence the CoMo exception.
    Since you are doing sounds.clear(); towards the end, you can remove the sounds.remove(resource); line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have HashMap 1, which contains 5 keys, all of which have Hashmaps as
I have a HashMap in Java, the contents of which (as you all probably
I have this HashMap: HashMap<String, Integer> m which basically stores any word (String) and
I have my HashMap, private final HashMap<Integer, Poll> pollmap = new HashMap<Integer, Poll>(); and
I have a static hashmap which I am using to cache objects in it.
I have a HashMap: private HashMap<String, Integer> cardNumberAndCode_ = new HashMap<String, Integer>(); And later
I have a HashMap with millions of entries. Need to retrieve all entries whose
I have a HashMap object that I am getting on a JSP page. HashMap<Integer,Gift_product>
I have a HashMap of a following structure: HashMap<Integer, int[]> map = new HashMap<Integer,
I have a HashMap<String,String> and there is a static method which returns this map

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.