Solved Concurrent modification

Discussion in 'Plugin Development' started by The Fancy Whale, Oct 12, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    The Fancy Whale

    I am working on making cooldowns. However, I keep getting throw a concurrent modification error. This error is due to the for loop I'm using then I modify it. However, I'm not sure of any other way to do it. Here is the code:
    Code:java
    1. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    2. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    3. @Override
    4. public void run() {
    5. for (UUID u : playerz.keySet()){ //playerz is a hashmap
    6. for (String s : playerz.get(u).keySet()){//players.get(u) returns a hashmap and line that is causing the error
    7. int i = playerz.get(u).get(s);
    8. i--;
    9. if (i <= 0){
    10. playerz.get(u).remove(s);
    11. if (playerz.get(u).isEmpty()){
    12. playerz.remove(u);
    13. }
    14.  
    15. }else{
    16. playerz.get(u).put(s, i);
    17. }
    18. }
    19. }
    20. }
    21. }, 0L, 20L);

    Error:
    Code:
    12.10 21:13:59 [Server] INFO at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [cbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks][/COLOR]
    [COLOR=#2c2c2c]12.10 21:13:59 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:469) [cbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks][/COLOR]
    [COLOR=#2c2c2c]12.10 21:13:59 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:558) [cbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks][/COLOR]
    [COLOR=#2c2c2c]12.10 21:13:59 [Server] INFO at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:258) [cbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks][/COLOR]
    [COLOR=#2c2c2c]12.10 21:13:59 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:600) [cbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks][/COLOR]
    [COLOR=#2c2c2c]12.10 21:13:59 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:349) [cbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks][/COLOR]
    [COLOR=#2c2c2c]12.10 21:13:59 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftTask.run(CraftTask.java:53) ~[cbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks][/COLOR]
    [COLOR=#2c2c2c]12.10 21:13:59 [Server] INFO at tfw.CC.Main$1.run(Main.java:33) ~[?:?][/COLOR]
    [COLOR=#2c2c2c]12.10 21:13:59 [Server] INFO at java.util.HashMap$KeyIterator.next(Unknown Source) ~[?:1.8.0_05][/COLOR]
    [COLOR=#2c2c2c]12.10 21:13:59 [Server] INFO at java.util.HashMap$HashIterator.nextNode(Unknown Source) ~[?:1.8.0_05][/COLOR]
    [COLOR=#2c2c2c]12.10 21:13:59 [Server] INFO java.util.ConcurrentModificationException[/COLOR]
    [COLOR=#2c2c2c]12.10 21:13:59 [Server] INFO [21:13:59 WARN]: [CommandCooldown] Task #21023 for CommandCooldown v1.0 generated an exception

    Thanks![/CODE][/COLOR]
     
  2. Offline

    rbrick

  3. Offline

    The Fancy Whale

  4. Offline

    adam753

    ConcurrentModificationExceptions happen when you add or remove elements from a collection (HashMap in this case) while iterating over it. Example:
    Code:java
    1. HashMap<String, String> map = new HashMap<String, String>();
    2. //...
    3. for(String key : map.keySet()) {
    4. map.remove(key); //ConcurrentModificationException!
    5. }


    There are a few ways around this but my preferred way is to clone the map, iterate over the clone, and modify the original.
    Code:java
    1. HashMap<String, String> map = new HashMap<String, String>();
    2. //...
    3. HashMap<String, String> clonedMap = new HashMap<String, String>(map); //I think this works?
    4. for(String key : clonedMap.keySet()) {
    5. map.remove(key);
    6. }
     
    Europia79 likes this.
  5. Offline

    rbrick

    Sorry i misunderstood.
     
  6. Offline

    The Fancy Whale

    rbrick adam753 Fixed with a list iterator. Works like a charm! Thanks for the help!
     
  7. Offline

    rbrick

Thread Status:
Not open for further replies.

Share This Page