Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
This question is a more special case of the problem described (and solved) in this question. I have two methods, stopAndRemove(ServerObject server) and a close() method. The later should close all servers and remove them from the server list. The list is defined as List<ServerObject> server. I do not want to have almost the same code from stopAndRemove in closeCurrentlyOpen, so I want...
I'm curious to know what other Java programmers feel is their favorite part of the language, why they feel that way, and why other programmers should want an intimate knowledge of it as well. I'm looking for reasons like simplicity, performance, etc. Thanks.
In the current implementation of CPython, there is an object known as the "GIL" or "Global Interpreter Lock". It is essentially a mutex that prevents two Python threads from executing Python code at the same time. This prevents two threads from being able to corrupt the state of the Python interpreter, but also prevents multiple threads from really executing together. Essentially, if I do this:...
I have a list in which I'd like to keep several head pointers. I've tried to create multiple ListIterators on the same list but this forbid me to add new elements in my list... (see Concurrent Modification exception). I could create my own class but I'd rather use a built-in implementation ;) To be more specific, here is an inefficient implementation of two basic operations and the one which ...
I found this statement if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception. at http://download.oracle.com/javase/6/docs/api/java/util/ConcurrentModificationException.html. I found the concurrent modification is even thrown in below code List<Employee> lista= new ArrayList(); Employee emp1...
I'm currently working on a multi-threaded application, and I occasionally receive a concurrently modification exception (approximately once or twice an hour on average, but occurring at seemingly random intervals). The faulty class is essentially a wrapper for a map -- which extends LinkedHashMap (with accessOrder set to true). The class has a few methods: synchronized set(SomeKey key, SomeVa...
The following mockup code ends up in ConcurrentModificationException, that happens (as i understand it), due to the fact that i am iterating over a set, which i am modifying. Set<String> data = new HashSet<String>(); data.add("a=1"); data.add("b=2"); data.add("c=3"); data.add("d=4"); for (String s : data) { data.remove(s); } But why is it exactly? Please help clarify
Can i remove an element while enumerating through a Properties object ?
The problem occurs at Element element = it.next(); And this code which contains that line, is inside of an OnTouchEvent for(Iterator<Element> it = mElements.iterator(); it.hasNext();){ Element element = it.next(); if(touchX > element.mX && touchX < element.mX + element.mBitmap.getWidth() && touchY > element.mY ...
I am getting this error everytime my Observers are traversed. @Override public void notifyObservers(ModelViewInterface model) { for(Observer<ModelViewInterface> o : this.observers) o.notify(model); } GWT does not have threads, so it is not a synchronization issue. It seems to happen after I press a button, any ideas of how to avoid this error?
I have a very simple snippet of code that populates a vector, iterates through it, and then clears it. Here is basically what I'm trying in principle: vector v = new Vector(); v.add(1); v.add(2); v.add(3); ListIterator iter = v.listIterator(); while (iter.hasNext()) { System.out.println(iter.next()); } v.clear() But I get a ConcurrentModificationException. From reading up on this, ap...
I have this little piece of code and it gives me the concurrent modification exception. I cannot understand why I keep getting it, even though I do not see any concurrent modifications being carried out. import java.util.*; //package p2; public class Class1 { public Class1() { } public static void main(String[] args) { Class1 class1 = new Class1(); List <String...
So, I'm writing a 2D Java game engine based on LWJGL. (It'll be open source, but when it's a bit more polished. :P) I'm pretty far along, but as I'm trying to go through another polish I've decided I need some outside opinion on a data structure I'm using. The structure is called an UpdateList<(generic)>. Basically, I want a fully dynamic list of objects to represent all objects in a game...
I am iterating over, and modifying a map (which is created from an existing group of enum objects) like the following: public class Dispenser { private Map<Ingredient, Integer> availableIngredients = new EnumMap<Ingredient, Integer>(Ingredient.class); public void orderSandwich(SandwichType sandwichType) { Map<Ingredient, Integer> buffer = ne...
I caught an exception with multi thread programing. I saw it at first, but I don't know how to solve it. then I search about ConcurrentModificationException then it says ConcurrentModificationException should be used only to detect bugs What does it mean? Maybe it says "you should only catch this exception" doesn't it?? or not?? Should I solve it??
could not find anything on this, wondering if anyone knew about this or a possible workaround. I am using JDOM and working with an xml schema. I have created a List of which are just xml tags. The algorithm's aim is to iterate through the List of elements and remove the element if a condition is met (in this case if it starts with a certain string). See below: for (Element appinfo : appinfos...
The idea: I have a JAX-RS webservice servlet (Object called webServlet) which instantiates a data collecting Object dataCollector and passes this object on to multiple threads in their constructor. These threads query websites for results and then call the dataCollector.add(result) method to add the results to a Queue within the shared dataCollector. I have two questions regarding this idea: ...
The problem is as follows: Write a static method subsets that uses recursive backtracking to find every possible sub-list of a given list. A sub-list of a list L contains 0 or more of L's elements. Your method should accept a List of strings as its parameter and print every sub-list that could be created from elements of that list, one per line. For example, suppose a variable called list stor...
With the snippet below I am, attempting to process a spreadsheet, with the twist of needing to exclude ad hoc columns. I know the crude way I am doing it, put the exceptions in an ArrayList and process the list on each and ever increment over the current row columns is perverse, but you know just get it done. However I am getting the titled error, which I believe should never happen. I am ...
I am in a very peculiar state. I have a list something like below :- List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); Now when i do multiple type of traversing, like using advanced for, iterator, and normal for loop, below are the sample code snippets :- 1> Advanced Loop :- try { for(String a : list) { System.out.println(a); list.a...
I'm getting a "An exception occurred: java.util.ConcurrentModificationException" when I run this piece of code. Does anyone here see what the problem is? public void mudaDeEstado() { Luz luz = new Luz(); while(this.iterador.hasNext()) { luz = (this.iterador.next()); luz.defineEstado(!luz.acesa()); } } Thanks a lot!!
 /*
  * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package java.util;

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.

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

public class ConcurrentModificationException extends RuntimeException {
    
Constructs a ConcurrentModificationException with no detail message.
    }

    
Constructs a ConcurrentModificationException with the specified detail message.

Parameters:
message the detail message pertaining to this exception.
    public ConcurrentModificationException(String message) {
        super(message);
    }
New to GrepCode? Check out our FAQ X