Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:40:07+00:00 2026-06-13T04:40:07+00:00

I have encountered a problem that I cannot quite figure out. Would be super

  • 0

I have encountered a problem that I cannot quite figure out.
Would be super if you guys could help me out =)

I have a Lobby.Class that has a draw() Method:

public void draw() {
    //USE ITERATOR TO DRAW
    Iterator<Player> itr = players.iterator();
    while(itr.hasNext()) {
        Player player = itr.next();
        int off = players.indexOf(player); //THE CORRECT HEIGHT
        player.draw(off);
    }
}  

Note that this is a server/client game

The Lobby.Class has also 1 method called addPlayer()

public void addPlayer(String _playerName, String _class, String _string_id) {

    int _id = Integer.parseInt(_string_id);

    //CREATE THE NEW PLAYER
    Player new_player = new Player(x, y, _id, _playerName, _class);

    //IF THIS PLAYER IS ME
    if(new_player.getID() == id) {

        me = new_player;

    } else {

        //TELL THE NEW PLAYER I EXIST
        ClientValue.getClient().iExist(_id);

                    //THIS WILL SEND TO CLIENT THAT WILL SEND TO THE SERVER
                    //THAT WILL LOOK FOR THE ID AND SEND TO CLIENT OF THAT ID
                    //AND THE CLIENT WILL SEND TO LOBBY UPDATE PLAYERS()
    }

    players.add(new_player);

    chat.appendChat(chat.joinString(_playerName, _class));
}

The Lobby.Class has also 1 method called updatePlayers()

public void updatePlayers(String _playerName, String _class, String _string_id) {

    //THIS IS CALLED WHEN THE SERVER TELLS THIS PLAYER
    //ABOUT OTHER PLAYERS

    int _id = Integer.parseInt(_string_id);

    //CREATE THE NEW PLAYER
    Player new_player = new Player(x, y, _id, _playerName, _class);
    players.add(new_player);

}

Now my problem is the players List, when I enter the lobby addPlayer() will Run at the same time when 2 players join, or when the updatePlayers() are run with addPlayer()

The Exception i get is: ConcurrentModificationException

I am missing two methods, 1 for adding players with an Iterator if possible, 1 for removing players. So that I wont get the error for modifying the players List while going trough it or adding/removing it.

I have tried puzzling around with Iteror and ListIterator, but as I have never used them before, I am not sure what to do. I need some advice on how to do this.
Many thanks in advance =)

Solution:

private List<Player> players = Collections.synchronizedList(new ArrayList<Player>());

    public synchronized void playersList(String cmd, Player _player) {

    Iterator<Player> itr = players.iterator();

    if(cmd.equals("draw")) {

        while(itr.hasNext()) {

            Player player = itr.next();
            int off = players.indexOf(player);
            player.draw(off);
        }

    } else if(cmd.equals("add")) {

        players.add(_player);

    } else if(cmd.equals("remove")) {

        players.remove(_player);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:40:08+00:00Added an answer on June 13, 2026 at 4:40 am

    You need to synchronise access to the list, allowing only one thread to access the list at a time.

    You could use a synchronized block around each access point (including the entire paint method) or you could wrap the player list in a thread safe list using Collections#synchronizedList or, instead of an iterator, you could use the toArray method of players

    Player[] arrayOfPlayers = players.toArray(new Player[players.size()]);
    

    You are still going to synchronized the this call, but your only locking one line, not a whole loop

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Guys, please help me with the following problem. I have encountered the famous cannot
I have encountered an issue that I cannot seem to work out in regards
I'm currently exploring powershell capabilities, but I have encountered a problem that I have
I have encountered a most annoying problem that occurs on the PWD variable when
I have the following problem that other people must have encountered. I am working
I have encountered a problem in my application. I have two forms, one that
I have encountered the strange problem, that linux c++ compiler includes the files from
I have encountered a problem i cannot find a solution. I am using a
I would like to ask a help for the problem I encountered. My mysql
I have encountered a slightly unusual problem. Consider the following code: class parser {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.