PlayerListener PlayerMove event Synchronization Trouble

Discussion in 'Plugin Development' started by Jonbas, Feb 24, 2011.

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

    Jonbas

    Hey,

    I hope someone can give me some advice on this problem I'm having. I'm trying to learn synchronization as I go here, and it seems I'm missing something.

    Here's the gist of what I want the program to do:

    onPlayerMove:
    -check new location for which regions the player is inside
    -if the player is inside a new region, add it to the player's list
    -check the player's list to see if there are any extra regions listed and remove them

    But when I run the code, i'm getting java.util.ConcurrentModificationException in the console. And the player messages tell me that every time I take a step I've entered and then left the region.

    The code works fine if I comment out the third step of the program and don't check to see if I've left a region and remove it from the list.

    Here's my git for this https://github.com/Jonbas/LocalShops and the problem is coming from the ShopsPlayerListener.java object.

    Thanks for any feedback, I really appreciate it.

    Jonbas
    --- merged: Feb 25, 2011 12:32 AM ---
    Okay, so I walked away for 5 minutes and found that this bit of code is always false:

    Code:
    //check the tree search results to see player is no longer in a shop.
    if(!res.results.contains(checkShopName)) {
      PlayerData.removePlayerFromShop(player, checkShopName);
      notifyPlayerLeftShop(player, checkShopName);
    }
    So... that may have a lot to do with it. The things you can't see when you're banging your head against them, I guess.
    --- merged: Feb 25, 2011 12:40 AM ---
    I was right, that fixed the constant adding and removing from the region list. But I still get:
    java.util.ConcurrentModificationException

    from:

    ShopsPlayerListener.java:71

    Any help would be appreciated.

    Cheers.
     
  2. Offline

    void420

    This is the most frequent cause:

    i.e. you are using iterators while modifying your data structure through it's own remove / add methods instead of the iterator's (that is what you are doing implicitly in your for loop + remove calls)

    Solutions to this problem is to refactor your code to remove items through your iterator or use a Concurrent* data structures
     
  3. Offline

    Jonbas

    Ahh, thank you very much. That was exactly the solution I needed. Replaced the failing part with something like this:

    Code:
    synchronized(list) {
        Iterator itr = list.iterator();
        while( itr.hasNext() ) {
            String value = itr.next().toString();
            if(value.equalsIgnoreCase(searchString)) {
                itr.remove();
                break;
            }
        }
    }
    And it works perfectly now. I'll probably be able to release my rebuild of Nijikokun's iCShop tomorrow.
     
Thread Status:
Not open for further replies.

Share This Page