Please navigate to the bottom of the page for Table of Contents

Sunday, June 19, 2011

Explain JAVA ConcurrentModificationException and solutions to fix it

The java.util Collection classes are fail-fast, which means that if one thread changes a collection while another thread is traversing it through with an iterator the iterator.hasNext() or call will throw ConcurrentModificationException. Even the synchronized collection wrapper classes SynchronizedMap and SynchronizedList are only conditionally thread-safe, which means all individual operations are thread-safe but compound operations where flow of control depends on the results of previous operations may be subject to threading issues.

Problem: Review the code below which may throw a ConcurrentModificationException and propose solutions to fix the issue.

Collection<String> myStr = new ArrayList<String>(10);

for (Iterator it = myStr.iterator(); it.hasNext();)
String myObject = (String);
if (1 == 1) // some condition
myStr.remove(myObject); //can throw ConcurrentModificationException


Answer 1: The list can be converted to an array with list.toArray() and iterate on the array. This approach is not
recommended if the list is large.

Answer 2: The entire list can be locked while iterating by wrapping your code within a synchronized block. This approach adversely affects scalability of your application if it is highly concurrent.

Answer 3: JDK 1.5 gives you ConcurrentHashMap and CopyOnWriteArrayList classes, which provide much better scalability and the iterator returned by ConcurrentHashMap.iterator() will not throw ConcurrentModificationException while preserving thread-safety.

Answer 4: Remove the current object via the Iterator “it” which has a reference to the underlying collection “myStr”. The Iterator object provides it.remove() method for this purpose.


  1. Very informative and helpful, thank you!


    1. The war between humans, orcs and elves continues earn to die . Lead your race through a series of epic battles, using your crossbow to fend off foes and sending out units to destroy castleshappy wheels . Researching and upgrading wisely will be crucial to your success! There are 5 ages total and each one will bring you new units to train to fight in the war for you cause.
      earn to die 2
      Whatever you do, don’t neglect your home base because you cannot repair it and once it is destroyed, you lose! Age of War is the first game of the series and really sets the tone for the Age of War games . Also try out the Age of Defense series as it is pretty similar.
      In this game, you start at the cavern men’s age, then evolvetank trouble ! There is a total of 5 ages, each with its units and turrets. Take control of 16 different units and 15 different turrets to defend your base and destroy your enemy.
      The goal of the game also differs depending on the level. In most levels the goal is to reach a finish line or to collect tokens. Many levels feature alternate or nonexistent goals for the player. The game controls are shown just under gold miner. Movement mechanisms primarily include acceleration and tilting controls. cubefield
      It consists of a total of 17 levels and the challenge you face in each level increases as you go up. unfair mario The game basically has a red ball that has to be moved across the various obstacles in its path to the goal. slitherio

  2. Thank you for the info. Answers with code should have been much more helpful.

  3. Very helpful article..thanks for it

  4. we are offering Apps for Android & IOS System in affordable price...........

    web design services

  5. A good blog. Thanks for sharing the information. It is very useful for my future. keep sharing
    red ball 2 | duck life 2 | happy wheels | Red Ball | Red ball 3 | Flash Games| Tank trouble

  6. I'm happy to read this article.Thanks for your information! Keep sharing..
    erp in chennai | cloud erp software in chennai

  7. I needed to thank you for this extraordinary read!! I unquestionably appreciated each and every piece of it. I need to bookmarked to look at new stuff on your post.
    Check Vanilla Prepaid Gift Card

  8. I was able to find good information from your blog posts. onsite mobile repair bangalore Great article! We will be linking to this particularly great content on our site. Keep up the great writing. asus display repair bangalore That is a great tip especially to those new to the blogosphere. Simple but very accurate info… Thanks for sharing this one. A must read post! huawei display repair bangalore

  9. Good site you have here.. It’s hard to find quality writing like yours nowadays. I seriously appreciate people like you! Take care!! online laptop repair center bangalore This is a topic that's close to my heart... Thank you! Exactly where are your contact details though? dell repair center bangalore

  10. I really love your site.. Great colors & theme. Did you make this web site yourself? Please reply back as I’m hoping to create my very own blog and would love to know where you got this from or exactly what the theme is named. Thanks! macbook repair center bangalore Everyone loves it when individuals get together and share opinions. Great website, continue the good work! acer repair center bangalore

  11. شركة تنظيف بالرياض الفيلا الجديده ؛ من خلال تنظيف و غسيل الموكيت و السجاد و الكليم و إزالة بقع الطعام منها أو التخلص من الاتربه و الغبار في وقت قياسي جدا ، كما خدمة الغسيل شقق فلل مجالس موكيت كنب مساجد خزانات منازل غسيل شقق
    غسيل فرشات غسيل فلل

    شركة تنظيف مجالس
    شركة تنظيف بالرياض توفير كافه الادوات والمعدات اللازمه في عمليه النظافة لحصول العميل علي منزل نظيف خالي من اي اوساخ بعمالة مدربة باسعار مميزة نظافة شقق موكيت سجاد كنب ستائر فلل منازل مجالس واجهات شقق مجالس كنب سجاد خزانات
    تنظيف فلل
    شركة تنظيف خزانات
    شركة تنظيف فلل ومنازل وشقق
    تنظيف مجالس فرشات ستائر كنب بالرياض

    تقدم خدمات
    تعقيم منازل
    نظافه عامه وخدمات المنزلى
    مكافحة حشرات

    حلى بلاط تلميع زوجتك وتلميع اثاث
    مكيفات فك وتركيب مكيفات
    شركة غسيل خزانات
    تعقيم شامل للمنزل
    شركة تنظيف بحائل
    شركة تنظيف بالرياض

    تهتم بتنظيف شقق فلل خيام استراحات

    تنظيف مكيفات كنب سجاد

    مجالس فرشات مكافحة حشرات

    تنظيف عزل خزانات

    تعقيم فلل مكاتب شركات جلي بلاط سيراميك

    بارخص الاسعار

  12. This comment has been removed by the author.

  13. I have learnt various good stuff right here, and I’m sure everyone will get advantage of it.
    web design studio