Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm using a hashmap as an infinite map for a tile based game. I'm trying to access all values in a hashmap from another thread and changing the content at the same time. But I'm getting a java.util.ConcurrentModificationException. How can I do this without getting an exception? I'm getting the exception in this line:

Object[] objects = hashMap.values().toArray();

share|improve this question

closed as off-topic by Byte56 Oct 18 '13 at 13:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Is there a ConcurrentHashMap you can use instead? –  Kurley Oct 18 '13 at 10:03
    
First thing you should do before asking a question like this is to Google the error message. You would have found plenty of answers at stackoverflow.com. –  Byte56 Oct 18 '13 at 13:27

2 Answers 2

instead of using Collections.synchronizedMap you can use a java.util.ConcurrentMap implementation

I suggest using the hashmap implementation

ConcurrentMap<String, String> map = new ConcurrentHashMap<String, String>();

exposing the ConcurrentMap interface allows use of the test and update style atomic operation

share|improve this answer
    
When using a ConcurrentMap I'm getting many null pointer exceptions. Is there a way to convert a normal HashMap to a list or an array without getting the ConcurrentModificationException? –  user1990950 Oct 18 '13 at 11:34
    
@user1990950 you may need to rethink your design, or add more checks –  ratchet freak Oct 18 '13 at 11:54
    
Ok but is a ConcurrentMap efficient? –  user1990950 Oct 18 '13 at 12:00
    
@user1990950 much more efficient than using Collections.synchronizedMap –  ratchet freak Oct 18 '13 at 12:02
    
Ok I got rid of the null pointer exceptions, but it made my game completely unplayable. Is there an alternative to Hashmaps for infinite dynamic maps? –  user1990950 Oct 18 '13 at 12:20

You get java.util.ConcurrentModificationException when you try to access the HashMap from more than one threads and at least one thread tries to modify it.

If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) Java HashMap

One way to solve it, is to wrap the Map using the Collections.synchronizedMap method. This way you will restrict the access to the HashMap only to one thread at a time ( call to a HashMap function) . Keep in mind that synchronizing the access to the map will make the calls a lot slower.


(e.x. of Collections.synchronizedMap)

Map<String, String> map = Collections.synchronizedMap(new HashMap<String, String>());
share|improve this answer
    
Is there a faster version? Just one thread modifies the values the other read them. –  user1990950 Oct 18 '13 at 10:27
    
It depends on what you are trying to do. This is basically a multi-threading problem. If you write a few data at the map you could just stick with that implementation. Another solution is to use message-passing between the threads. There are a lot of different solutions, that each maps better at a different problem. –  Anastasios G Oct 18 '13 at 10:46
    
Another way is to allow the first thread to finish writing all the data to the HashMap and then signals the second thread to begin reading. If it is allowed by your solution it would be fast and avoid synchronizedMap. –  Anastasios G Oct 18 '13 at 10:50
    
Ok I've got a 2 threads that update the tiles, 1 thread that adds new tiles and drawing is done in the main thread, but I'm not converting the map into an array when drawing. –  user1990950 Oct 18 '13 at 10:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.