Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Best way avoiding ConcurrentModifications

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Best way avoiding ConcurrentModifications

    Hey.

    I have a HashMap like this:
    HashMap<Player, Integer> players = new HashMap<Player, Integer>();

    The int is for the amount of seconds the player has left. Each second one second is removed from the person: (this is inside a thread)
    for (final Iterator<Player> it = players.keySet().iterator(); it.hasNext(); ) {
        final Player player = it.next();
     
        if (player.isOnline()) {
            int seconds = getTimeLeft(player);
     
            if (seconds <= 0) {
                if (!(player.isDead())) {
                    player.setHealth(0); // This kills the player
                }
            }
     
            setTimeLeft(player, --seconds);
        } else {
            it.remove();
        }
    }

    If the player has 0 seconds or less, the player is killed and later runs a method to update everything correspondingly. The player can also die naturally, either by another player or from eg. starvation, so thats why I can't necessarily add the check directly to the thread / loop. This is the code which is run when the player dies:

    public void playerKilled(Player player) {
            // Some validation before this
            players.remove(player);
            spectators.add(player);
    }

    The problem with this is that it always throws a ConcurrentModificationException. I changed the HashMap to ConcurrentHashMap and that fixed it, but that forces me add a ton of null checks everywhere and it ends up extremely ugly and occasionally fails for some reason.

    So the question is: What would be the best solution to avoid both of these errors while retaining the functionality?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Best way avoiding ConcurrentModifications

    This seems like a really roundabout way to accomplish your goal. Why don't you just record the player's start time and then figure out the remaining time from that?

    //init
    long startTime = System.currentTimeMillis();
    long lifetime = 120*1000; //2 minutes

    //update
    if(System.currentTimeMillis() - startTime > lifetime){
    die();
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Best way avoiding ConcurrentModifications

    Theres 24 players and I want to report to each of them how much time is left each second. It's a plugin for a game, so if you kill someone you get the time which the other person had, and so on. So that wont work.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Best way avoiding ConcurrentModifications

    Quote Originally Posted by Jergetson View Post
    if you kill someone you get the time which the other person had, and so on. So that wont work.
    This seems to have nothing to do with time running out. Looks like something more related to combat code where damage numbers are being passed around. When the player is killed, as a last attack, call a method youKilledMe(myTimeLeft) and rather than taking damage, that method bumps the winning players time. Something of this sort or not, still can not see how its related to the cme at hand.
    Is it necessary for all of these threads to modify the set?
    Why are these threads not modifying the objects within the set instead of the set?
    Do dead players have to be removed or can they be ignored prior to lengthy code?
    Will new players join before the game is over?
    Do all players have the same time remaining?
    Is 24 the maximum number of players?
    There are many many possible solutions to the problem, the question is which one fits best. The subset of questions above can grow various ways depending on answers to some of them.
    Think about what needs to be done when and try to organize the flow with as little concurrency as is necessary.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Best way avoiding ConcurrentModifications

    Quote Originally Posted by Jergetson View Post
    Theres 24 players and I want to report to each of them how much time is left each second. It's a plugin for a game, so if you kill someone you get the time which the other person had, and so on. So that wont work.
    I'm not really sure why you think that means a more direct approach won't work. Just put the time addition code in the check for whether a player has been killed. No need for convoluted threading.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 7
    Last Post: February 2nd, 2013, 10:07 AM
  2. Avoiding concurrent modification exception?
    By Twon23 in forum Collections and Generics
    Replies: 4
    Last Post: November 12th, 2012, 10:34 AM
  3. avoiding OutOfMemoryException
    By ragavan in forum Exceptions
    Replies: 3
    Last Post: July 27th, 2011, 12:25 PM

Tags for this Thread