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 9135063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:46:34+00:00 2026-06-17T08:46:34+00:00

HashMap’s javadoc states: if the map is structurally modified at any time after the

  • 0

HashMap’s javadoc states:

if the map is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a ConcurrentModificationException.

I built a sample code that, based on the specification, is supposed to fail almost immediately and throw a ConcurrentModificationException;

  • It does fail immediately as expected with Java 7
  • but it (seems to) always work with Java 6 (i.e. it does not throw the promised exception).

Note: it sometimes does not fail with Java 7 (say 1 time out of 20) – I guess it has to do with thread scheduling (i.e. the 2 runnables are not interleaved).

Am I missing something? Why does the version run with Java 6 not throw a ConcurrentModificationException?

In substance, there are 2 Runnable tasks running in parallel (a countdownlatch is used to make them start approximately at the same time):

  • one is adding items to the map
  • the other one is iterating over the map, reading the keys and putting them into an array

The main thread then checks how many keys have been added to the array.

Java 7 typical output (the iteration fails immediately):

java.util.ConcurrentModificationException
MAX i = 0

Java 6 typical output (the whole iteration goes through and the array contains all the added keys):

MAX i = 99

Code used:

public class Test1 {

    public static void main(String[] args) throws InterruptedException {
        final int SIZE = 100;
        final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(1, 1);
        map.put(2, 2);
        map.put(3, 3);
        final int[] list = new int[SIZE];
        final CountDownLatch start = new CountDownLatch(1);
        Runnable put = new Runnable() {
            @Override
            public void run() {
                try {
                    start.await();
                    for (int i = 4; i < SIZE; i++) {
                        map.put(i, i);
                    }
                } catch (Exception ex) {
                }
            }
        };

        Runnable iterate = new Runnable() {
            @Override
            public void run() {
                try {
                    start.await();
                    int i = 0;
                    for (Map.Entry<Integer, Integer> e : map.entrySet()) {
                        list[i++] = e.getKey();
                        Thread.sleep(1);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        };
        ExecutorService e = Executors.newFixedThreadPool(2);
        e.submit(put);
        e.submit(iterate);
        e.shutdown();

        start.countDown();
        Thread.sleep(100);
        for (int i = 0; i < SIZE; i++) {
            if (list[i] == 0) {
                System.out.println("MAX i = " + i);
                break;
            }
        }
    }
}

Note: using JDK 7u11 and JDK 6u38 (64 bits version) on an x86 machine.

  • 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-17T08:46:35+00:00Added an answer on June 17, 2026 at 8:46 am

    If we will look into HashMap sources and compare them between Java 6 and Java 7 we will see such interesting difference:

    transient volatile int modCount; in Java6 and just transient int modCount; in Java7.

    I’m sure that it is cause for different behavior of mentioned code due to this:

            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
    

    UPD: It seems to me, that this is a known Java 6/7 bug: https://bugs.java.com/bugdatabase/view_bug?bug_id=6625725 which was fixed in latest Java7.

    UPD-2: Mr. @Renjith said, that he just tested and did not found any difference in behavior of HashMaps implementation. But I just tested it too.

    My test was:

    1. I have created HashMap2 class, which is absolutely copy of HashMap from Java 6.

    One important thing is we need to introduce here 2 new fields:

    transient volatile Set<K>        keySet = null;
    

    and

    transient volatile Collection<V> values = null;
    
    1. Then I used this HashMap2 in test of this question and run it under Java 7

    Result: it works like such test under Java 6, i.e. there isn’t any ConcurentModificationException.

    That all proves my conjecture. Q.E.D.

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

Sidebar

Related Questions

I want to use HashMap or any Map for my purpose. I want to
I have a hashmap and an iterator that iterates through four alternatives and the
I created a hashmap as shown below: Map<String, String> streetno = new HashMap<String, String>();
This is my hashmap : Map<String,String> unsortMap = new HashMap<String,String>() this map contains values
Possible Duplicate: Java.util.HashMap — why HashMap extends AbstractMap and implement Map? In java to
I have a hashmap containing objects created via a constructor. These objects are in
HashMap allows one null key and any number of null values. What is the
I have a HashMap with two Strings Map<String, String> ldapContent = new HashMap<String, String>
I m using hashmap for a listview. map = new HashMap<String, Object>(); map.put(name, R.string.information);
HashMap<String, int> doesn't seem to work but HashMap<String, Integer> does work. Any ideas why?

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.