concurrentmodification

ConcurrentModificationException in unmodifiable collection [duplicate]

一曲冷凌霜 提交于 2019-12-30 08:24:09
问题 This question already has answers here : Why is a ConcurrentModificationException thrown and how to debug it (7 answers) Closed 10 months ago . I have this code below, and I'm getting a ConcurrentModificationException by executing the following line: filterCardsToDevice(getCollection()); the code: private List<MyClass> filterCardsToDevice(Collection<MyClass> col) { final List<MyClass> newList = new ArrayList<MyClass>(); for (MyClass myObj : col) { long id = myObj.getId(); if (id < 0 || id >

concurrent modification on arraylist

限于喜欢 提交于 2019-12-25 18:27:08
问题 There are a lot of concurrent mod exception questions, but I'm unable to find an answer that has helped me resolve my issue. If you find an answer that does, please supply a link instead of just down voting. So I originally got a concurrent mod error when attempting to search through an arraylist and remove elements. For a while, I had it resolved by creating a second arraylist, adding the discovered elements to it, then using removeAll() outside the for loop. This seemed to work, but as I

I keep getting java.util.concurrentmodificationexception.. How to fix this?

久未见 提交于 2019-12-24 06:06:16
问题 I've been working on this snippet of code. Here is the pseudocode of what I want to happen: a.check if sections(which is a list) size is 0. b.if sections size is 0, then automatically enroll the student to the section by calling sections.add(newSection) c.else if sections size is not zero, check for conflicts with schedule d.if there are no conflicts, then enroll the student to the section by calling sections.add(newSection) e.else do nothing Java keeps throwing "java.util

Can get, put & remove elemetn in HashMap without iteration cause ConcurrentModificationException?

混江龙づ霸主 提交于 2019-12-23 23:52:47
问题 I have a static hashMap, shared with multiple threads. I am not iterating the map at all but just uses the get , put , remove . Is it safe from ConcurrentModificationException ? The method looks like this private static Map<Long, Integer> TRACKER = new HashMap<Long,Integer>(); public static void track(Long tid, boolean b) { if (b) { if (TRACKER.containsKey(tid)) { TRACKER.put(tid, TRACKER.get(tid) + 1); } else { TRACKER.put(tid, 1); } } else { Integer n = TRACKER.get(tid); if (n != null) { n

Concurrent Modification Exception in Java [duplicate]

£可爱£侵袭症+ 提交于 2019-12-23 19:23:51
问题 This question already has answers here : Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop (24 answers) Closed 2 years ago . I am getting the ConcurrentModificationException while executing this code. I am unable to figure out why it is happening? private void verifyBookingIfAvailable(ArrayList<Integer> list, int id) { Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()) { int value = iterator.next(); if (value == id)

Collections.sort method sometimes throws ConcurrentModificationException in multithreaded environment . List is not being modified structurally

喜欢而已 提交于 2019-12-23 17:16:25
问题 package CollectionsTS; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; public class ArrayListTS { public static void main(String[] args) { HashSet<Integer> hset = new HashSet<Integer>(); for (int i = 0; i <= 1000; i++) { hset.add(i); } MyRunnable mr = new MyRunnable(); mr.addElements(hset); Thread t1 = new Thread(mr,"t1"); Thread t2 = new Thread(mr,"t2"); Thread t3 = new Thread(mr,"t3"); t1.start(); t2.start(); t3.start(); } } class

Java Programming Error: java.util.ConcurrentModificationException

假装没事ソ 提交于 2019-12-23 13:09:14
问题 I'm writing a program as part of tutorial for a beginner Java student. I have the following method and whenever I run it, it gives me the following exception: java.util.ConcurrentModificationException at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372) at java.util.AbstractList$Itr.next(AbstractList.java:343) at Warehouse.receive(Warehouse.java:48) at MainClass.main(MainClass.java:13) Here's the method itself, within the class Warehouse: public void receive(MusicMedia

How do you interate over a Collection<T> and modify its items without ConcurrentModificationException?

和自甴很熟 提交于 2019-12-23 05:26:05
问题 I need to do something like this... Collection<T> myCollection; ///assume it is initialized and filled for(Iterator<?> index = myCollection.iterator(); index.hasNext();) { Object item = index.next(); myCollection.remove(item); } Obviously this throws ConcurrentModificationException... So I have tried this but doesn't does seem elegant/efficient and throws a Type safety: Unchecked cast from Object to T warning Object[] list = myCollection.toArray(); for(int index = list.length - 1; index >= 0;

Why no ConcurrentModificationException when one thread iterating (using Iterator) and other thread modifying same copy of non-thread-safe ArrayList

断了今生、忘了曾经 提交于 2019-12-23 04:45:55
问题 Some background: When a collection is iterated using Iterator then there could java.util.ConcurrentModificationException because under the hoods when the Iterator object is created then the modification count ( modCount ) of the collection or ArrayList is captured and on each iteration using Iterator.next() it is checked if modCount has changed, if so then throw java.util.ConcurrentModificationException . On creating iterator object (from ArrayList 's Iterator implementation): int

ConcurrentModificationException and HashSet.iterator()

僤鯓⒐⒋嵵緔 提交于 2019-12-22 09:58:12
问题 I have a for loop like for (int neighbour : neighbours) { Where I may modify neighbours within the loop. Found that thats the cause of ConcurrentModificationException . And read from https://stackoverflow.com/a/8189527/292291 Hence if you want to modify the list (or any collection in general), use iterator , because then it is aware of the modifications and hence those will be handled properly. So I tried: neighboursItr = neighbours.iterator(); while (neighboursItr.hasNext()) { // try