concurrentmodification

Collection - Iterator.remove() vs Collection.remove()

寵の児 提交于 2019-12-09 05:44:23
问题 As per Sun , "Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress." I have two questions : What makes this operation "Iterator.remove()" stable than the others ? Why did they provide a "Collection.remove()" method if it will not be useful in most of the use-cases? 回答1: If you're iterating over a collection and use: Collection.remove() you can get

Calling a method from within a synchronized method

懵懂的女人 提交于 2019-12-08 13:35:23
问题 I'm facing a strange problem which has made me wonder what exactly happens in a synchronized method. Let's say there is a method synchronized public void example(){ //...code int i=call(); //calling another method //...do something with i } Now while the call() method is being executed, can another object enter this synchronized example() method? So when the call() returns, there might be some ConcurrentModificationException? What to do in order to avoid problems? 回答1: No it can't. A

Another ConcurrentModificationException question

房东的猫 提交于 2019-12-08 06:57:03
问题 I've searched StackOverflow and there are many ConcurrentModificationException questions. After reading them, I'm still confused. I'm getting a lot of these exceptions. I'm using a "Registry" setup to keep track of Objects: public class Registry { public static ArrayList<Messages> messages = new ArrayList<Messages>(); public static ArrayList<Effect> effects = new ArrayList<Effect>(); public static ArrayList<Projectile> proj = new ArrayList<Projectile>(); /** Clears all arrays */ public static

MySQL atomic insert-if-not-exists with stable autoincrement

陌路散爱 提交于 2019-12-07 23:00:18
问题 In MySQL, I am using an InnoDB table that contains unique names, and IDs for those names. Clients need to atomically check for an existing name, insert a new one if it does not exist, and get the ID. The ID is an AUTO_INCREMENT value, and it must not increment out-of-control when checking for existing values regardless of the setting of "innodb_autoinc_lock_mode"; this is because very often the same name will be checked (e.g. " Alice "), and every now and then some new name will come along (e

Another ConcurrentModificationException question

孤者浪人 提交于 2019-12-06 15:12:28
I've searched StackOverflow and there are many ConcurrentModificationException questions. After reading them, I'm still confused. I'm getting a lot of these exceptions. I'm using a "Registry" setup to keep track of Objects: public class Registry { public static ArrayList<Messages> messages = new ArrayList<Messages>(); public static ArrayList<Effect> effects = new ArrayList<Effect>(); public static ArrayList<Projectile> proj = new ArrayList<Projectile>(); /** Clears all arrays */ public static void recycle(){ messages.clear(); effects.clear(); proj.clear(); } } I'm adding and removing objects

Why isn't this code causing a ConcurrentModificationException? [duplicate]

[亡魂溺海] 提交于 2019-12-06 02:14:17
This question already has an answer here: Why is a ConcurrentModificationException thrown and how to debug it 7 answers I was reading about ConcurrentModificationException and how to avoid it. Found an article . The first listing in that article had code similar to the following, which would apparently cause the exception: List<String> myList = new ArrayList<String>(); myList.add("January"); myList.add("February"); myList.add("March"); Iterator<String> it = myList.iterator(); while(it.hasNext()) { String item = it.next(); if("February".equals(item)) { myList.remove(item); } } for (String item

ConcurrentModificationException and HashSet.iterator()

送分小仙女□ 提交于 2019-12-05 20:57:09
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 disconnecting vertices neighbour = neighboursItr.next(); But that doesnt fix the problem. Why? Are you calling

Avoiding ConcurrentModificationException on List by making a shallow copy

て烟熏妆下的殇ゞ 提交于 2019-12-05 16:19:03
I have a class like the following: class Test { private LinkedList<Person> persons = new LinkedList<Person>; public synchronized void remove(Person person) { persons.remove(person); } public List<Person> getAllPersons() { // Clients may iterate over the copy returned and modify the structure. return new ArrayList<Person>(persons); } } persons may be modified concurrently: one is via remove() by one thread and two via the shallow copied instance returned by getAllPersons() . I have tested the above scenario in a multithreaded environment to see if I can avoid ConcurrentModificationException by

Multiple threads accessing one variable

蹲街弑〆低调 提交于 2019-12-05 12:04:51
I found this question in a textbook I am reading. The solution is given below it as well. I'm having trouble understanding how the minimum could be 2. Why couldn't a thread read 0, all other threads execute and it writes 1? And whether it is 1 or 2, the thread writing last must still complete its own loop? int n = 0; int main(int argc, char **argv) { for (i = 0; i < 5; i++) { int tmp = n; tmp = tmp + 1; n = tmp; } return 0; } If a single thread ran this application, you would expect the final output to be 5. What if 5 threads ran the same loop in parallel? What are the largest and smallest

why concurrent modification on foreach method but not on for loop

核能气质少年 提交于 2019-12-05 10:26:22
ArrayList<Integer> targets = new ArrayList<Integer>(); targets.add(2); targets.add(2); for (Integer testInt : targets ) { targets.add(1); } I am getting an concurrentModificationException,But with normal for loop. I am not getting any exception. in normal forloop like:- for(int i=0;i<target.size();i++) { System.out.println(target.get(i)); target.add(22); //no exception target.remove(2) // no exception } ForEach loop won't loop directly on your collection. It uses the iterator of your collection behind. you can see the iterator in your collections implementation. From Arraylist source code 735