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 9097539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:08:24+00:00 2026-06-17T00:08:24+00:00

Consider these two code samples: private final Player[] players = new Player[MAX_PLAYERS + 1];

  • 0

Consider these two code samples:

private final Player[] players = new Player[MAX_PLAYERS + 1];
private int playerCount;

public boolean addPlayer(Player player) {
    synchronized (players) {
        for (int i = 1; i < players.length; i++) {
            if (players[i] == null) {
                players[i] = player;
                playerCount++;
                player.setIndex(i);
                return true;
            }
        }
        return false;
    }
}

public void removePlayer(Player player) {
    synchronized (players) {
        players[player.getIndex()] = null;
        playerCount--;
    }
}

public Player[] getPlayers() {
    synchronized (players) {
        return players;
    }
}

public int getPlayerCount() {
    synchronized (players) {
        return playerCount;
    }
}

and…

private final AtomicReferenceArray<Player> players = new AtomicReferenceArray<Player>(MAX_PLAYERS + 1);
private final AtomicInteger playerCount = new AtomicInteger();

public boolean addPlayer(Player player) {
    for (int i = 1; i < players.length(); i++) {
        if (players.get(i) == null) {
            players.set(i, player);
            playerCount.incrementAndGet();
            player.setIndex(i);
            return true;
        }
    }
    return false;
}

public void removePlayer(Player player) {
    players.set(player.getIndex(), null);
    playerCount.decrementAndGet();
}

public AtomicReferenceArray<Player> getPlayers() {
    return players;
}

public AtomicInteger getPlayerCount() {
    return playerCount;
}

Now, I know that accessing an array normally is very efficient. However, I know that synchronization can be costly. On the other side, atomic operations do not need to be synchronized, but I would guess that players.get(i) is not as efficient as players[i]. So, which of these samples would give me best performance if I were to use it in a game setting? I have the server designed so that each new player has a thread dedicated to them. And every time they finish connecting and logging in, they add themselves to the list of players, via addPlayer(Player). When a player disconnects, they remove themselves from the list of players, via removePlayer(Player). Since these operations are invoked from different threads, synchronization is definitely needed.
So which should I use?

  • 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-17T00:08:25+00:00Added an answer on June 17, 2026 at 12:08 am

    Most of these questions on SO can be answered with “have you measured it” and can be very dependent upon your environment. However, whatever solution you choose:

    And every time they finish connecting and logging in, they add
    themselves to the list of players, via addPlayer(Player)

    the cost of connecting/logging in etc. across a network will massively dwarf any efficiency you’re concerned about.

    This assertion:

    However, I know that synchronization can be costly

    used to be more of a concern. Synchronisation nowadays is substantially less costly.

    For the above, I really wouldn’t worry about it. Get your solution working, and then determine if the above is sufficiently inefficient to warrant reworking. I see little in the above performance-related to cause me concern.

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

Sidebar

Related Questions

Consider the following code: public class Foo { private static final Object LOCK =
Consider the following code snippet: class Test { public int Length{ get; set; }
Consider this code: one.c: #include <stdio.h> int one() { printf(one!\n); return 1; } two.c:
Consider having these two codes: Code 1: Uri uri = Uri.parse(url); intent.setDataAndType(uri, audio/*); Code
please consider following code #include <iostream> using namespace std; class Digit { private: int
Consider the following code: class C { public: int operator-(int x) { return 3-x;
Consider these two classes mapped to the same table. One is readonly via mutable=false.
I ran across a compilation issue today that baffled me. Consider these two container
Consider these 2 pieces of code (you can assume execeptionObj is of type Object
Consider these two snippets: try: a+a=a except SyntaxError: print first exception caught . try:

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.