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 iterator.next() 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);
myStr.add("abc");
myStr.add("def");
myStr.add("ghi");
for (Iterator it = myStr.iterator(); it.hasNext();)
{
String myObject = (String)it.next();
System.out.println(myObject);
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.
Very informative and helpful, thank you!
ReplyDeleteTom
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.
Deleteearn 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
Thank you for the info. Answers with code should have been much more helpful.
ReplyDeleteGood,
DeleteSelenium Training In Chennai
Selenium Training In Chennai
Thanks, nice tips
ReplyDeleteVery helpful article..thanks for it
ReplyDeleteThank so much
ReplyDeletewe are offering Apps for Android & IOS System in affordable price...........
ReplyDeleteweb design services
A good blog. Thanks for sharing the information. It is very useful for my future. keep sharing
ReplyDeletered ball 2 | duck life 2 | happy wheels | Red Ball | Red ball 3 | Flash Games| Tank trouble
I'm happy to read this article.Thanks for your information! Keep sharing..
ReplyDeleteerp in chennai | cloud erp software in chennai
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.
ReplyDeleteCheck Vanilla Prepaid Gift Card
Nice information , i am looking for more
ReplyDeleteSelenium Training In Chennai
Selenium Training In Chennai
Data Science Training In Chennai
Machine Learning Course In Chennai
Data Science Course In Chennai
Protractor Training in Chennai
jmeter training in chennai
Rpa Training Chennai
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
ReplyDeleteGood 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
ReplyDeleteI 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
ReplyDeleteشركة تنظيف منازل بالرياض
ReplyDeleteشركة منازل بالرياض
شركة نظافة عامة بالرياض
شركة صيانه افران
تنظيف منازل بالرياض
شركة تنظيف منازل بالرياض
شركة صيانه افران
افضل شركة صيانه افران بالرياض
شركة تنظيف منازل بالرياض
شركة تنظيف خزنات بابها
Deleteشركة رش مبيدات بابها
شركة تنظيف مجالس وسجاد بابها
شركة تنظيف مجالس بابها
ارخص شركة تنظيف سجاد بخميس مشيط
شركة الصفرات
ارخص شركة تنظيف شقق بالرياض
شركة نركيب طارد حمام بالرياض
ارخص شركة رش مبيدات بالرياض
ارخص شركة مكافحة حشرات بالرياض
ارخص شركة تنظيف مكيفات بالرياض
شركة تنظيف مكيفات بالرياض
ارخص رشكة غسيل مكيفات بالرياض
شركة تنظيف منازل بالرياض
افضل شركة تنظيف منازل
شركة تصليح وتنظيف افران بالرياض
ReplyDeleteافضل شركة تنظيف افران
شركة صيانه افران
افضل شركة تنظيف افران
شركة صيانة افران بالرياض
تنظيف افران
صيانه افران بالرياض
شركة تنظيف بالرياض
افضل شركة صيانة افران بالرياض
شركة تنظيف بالرياض الفيلا الجديده ؛ من خلال تنظيف و غسيل الموكيت و السجاد و الكليم و إزالة بقع الطعام منها أو التخلص من الاتربه و الغبار في وقت قياسي جدا ، كما خدمة الغسيل شقق فلل مجالس موكيت كنب مساجد خزانات منازل غسيل شقق
ReplyDeleteغسيل فرشات غسيل فلل
شركة تنظيف مجالس
شركة تنظيف بالرياض توفير كافه الادوات والمعدات اللازمه في عمليه النظافة لحصول العميل علي منزل نظيف خالي من اي اوساخ بعمالة مدربة باسعار مميزة نظافة شقق موكيت سجاد كنب ستائر فلل منازل مجالس واجهات شقق مجالس كنب سجاد خزانات
تنظيف فلل
شركة تنظيف خزانات
شركة تنظيف فلل ومنازل وشقق
تنظيف مجالس فرشات ستائر كنب بالرياض
تقدم خدمات
تعقيم منازل
نظافه عامه وخدمات المنزلى
مكافحة حشرات
حلى بلاط تلميع زوجتك وتلميع اثاث
مكيفات فك وتركيب مكيفات
شركة غسيل خزانات
تعقيم شامل للمنزل
شركة تنظيف بحائل
شركة تنظيف بالرياض
تهتم بتنظيف شقق فلل خيام استراحات
تنظيف مكيفات كنب سجاد
مجالس فرشات مكافحة حشرات
تنظيف عزل خزانات
تعقيم فلل مكاتب شركات جلي بلاط سيراميك
بارخص الاسعار
nice blog. content is useful.
ReplyDeleteBest BBA college
thanks for the article
ReplyDeleteBest commerce college
This comment has been removed by the author.
ReplyDeleteGood Blog
ReplyDeleteBest BBA College
Đặt vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ giá bao nhiêu
lịch bay mỹ về việt nam
ve may bay tư duc ve viet nam
giá vé máy bay từ nhật về việt nam
Chuyến bay từ Hàn Quốc về Việt Nam
vé máy bay từ canada về việt nam giá rẻ
khách sạn cách ly ở vân đồn
I have learnt various good stuff right here, and I’m sure everyone will get advantage of it.
ReplyDeleteweb design studio
تمتلك افضل شركات تداول العملات في العالم العديد من المميزات
ReplyDeleteexcellent information. Full Stack Course In Pune
ReplyDeleteThanks for sharing it with us.
ReplyDeleteJava classes in Pune