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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:45:30+00:00 2026-06-02T05:45:30+00:00

Question Edited : HashSet and HashMap are fail-fast(but that is not gauranteed) as mentioned

  • 0

Question Edited :
HashSet and HashMap are fail-fast(but that is not gauranteed) as mentioned in code :

void goHashSet() {
        Set set = new HashSet();
        for (int i = 1; i <= 10; i++) {
            set.add(i);
        }
        Iterator i = set.iterator();
        while (i.hasNext()) {
            // set.add(16);//Exception in thread "main"
            // java.util.ConcurrentModificationException
            System.out.println("HashSet >>> itertor >>>" + i.next());
        }
    }

Now, i want the example and the collections which are fail-safe
as much as i know: ConcurrentHashMap,CopyOnWriteArrayList are fail-safe..but how to code it to show that they are fail-safe

Edit and Undestanding of what I want and how I achieved it :

if we use HashMap

void goHashMap() {
        Map mp = new HashMap();
        for (int i = 19; i <= 24; i++) {
            mp.put(i, "x");
        }
        Set setKeys = mp.keySet();
        Iterator i = setKeys.iterator();
        while (i.hasNext()) {
            // mp.put(499, "x");// Exception in thread "main"
            // java.util.ConcurrentModificationException
            System.out.println("HashMap >>> itertor >>>" + i.next());
        }
    }

we get ConcurrentMException

but the same code is done with ConcurrentHashMap, no error is there (non-multithreading enviro)

void goConcurrentHashMap() {
    Map mp = new ConcurrentHashMap();
    for (int i = 19; i <= 24; i++) {
        mp.put(i, "x");
    }
    Set setKeys = mp.keySet();
    Iterator i = setKeys.iterator();
    while (i.hasNext()) {
        mp.put(499, "x");
        System.out.println("HashConcurrentMap >>> itertor >>>" + i.next());
    }
}

whats more : in multithreading enviro the ConcurrentHashmap might get fail-fast and throw exception CME

  • 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-02T05:45:32+00:00Added an answer on June 2, 2026 at 5:45 am

    The API Javadoc (both for HashSet and HashMap) only makes a soft guarantee to fail-safety:

    Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

    The identical behaviour is explained by the fact that HashSet is implemented on top of a HashMap internally.

    Feel free to check out the source code, bundled with your JDK installation though.

    Update

    Now, i want the example and the collections which are fail-safe

    Have a look at the collections in the java.util.concurrent package.

    as much as i know: ConcurrentHashMap,CopyOnWriteArrayList are fail-safe..but how to code it to show that they are fail-safe

    The nature of concurrency is such that it is impossible to prove thread-safety (or fail-safety) by tests alone. You can only prove the opposite, i.e. that a specific class is not thread-safe (if that happens to be the case). This is exactly what makes concurrency hard.

    The closest to proving such things is thorough analysis of the code in question by one or more concurrency experts. Some static analysis tools can spot some issues too, but again, a clean static analysis result is no guarantee.

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

Sidebar

Related Questions

EDITED SO THE CODE IS CORRECT (THANKS TO ta.speot.is) - NEW QUESTION AT BOTTOM
I have edited this question but at the bottom of the program I have
Edited the question due to progressive insights :-) I am creating an app that
i edited the question it still does not work, the user writes appendix then
Ok: I edited my question: I heard somewhere that php language is written in
[EDIT] I edited my question with complete code and explanation and hope something can
I edited my question when I set the bounty. I want to Invoke/DllImport WSAAsyncSelect()
Edited question to make it a bit more specific. Not trying to base it
Edited Question : I am working on a multithreaded JMS receiver and publisher code
I am nearly ashamed to ask this, but... EDIT: I edited the question to

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.