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

  • Home
  • SEARCH
  • 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 9004903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:00:27+00:00 2026-06-16T01:00:27+00:00

I wrote a game in Java with a client and server side using sockets

  • 0

I wrote a game in Java with a client and server side using sockets and data input/output streams. The server side sometimes needs to send a message to all users in a “for” loop, but since writing to a socket can block I created a thread for each user that sends him messages (as well as another thread for each user that listens to incoming messages). The sending thread is built on this idea:

private ArrayList<Object> messages = new ArrayList<Object>(),
                          newMessages = new ArrayList<Object>();

public void run() {
    while (true) {
        for (Object message: messages) {
            try {
                if (message instanceof Byte)
                    out.writeByte((Byte)message);
                else if (message instanceof Boolean)
                    out.writeBoolean((Boolean)message);
                else if (message instanceof String)
                    out.writeUTF((String)message);
                else if (message instanceof Integer)
                    out.writeInt((Integer)message);
                else if (message instanceof Long)
                    out.writeLong((Long)message);
            } catch (IOException e) {}
        }
        synchronized (newMessages) {
            messages.clear();
            messages.addAll(newMessages);
            newMessages.clear();
        }
    }
}

public void write(Object message) {
    synchronized (newMessages) {
        newMessages.add(message);
    }
}

Unfortunately the run() method is constantly running, so I’d like to insert a sleep command, to achieve something like this:

private ArrayList<Object> messages = new ArrayList<Object>(),
                          newMessages = new ArrayList<Object>();

public void run() {
    while (true) {
        try {
            if (messages.isEmpty() && newMessages.isEmpty())
                sleep(0);
        } catch (InterruptedException e) {}
        for (Object message: messages) {
            try {
                if (message instanceof Byte)
                    out.writeByte((Byte)message);
                else if (message instanceof Boolean)
                    out.writeBoolean((Boolean)message);
                else if (message instanceof String)
                    out.writeUTF((String)message);
                else if (message instanceof Integer)
                    out.writeInt((Integer)message);
                else if (message instanceof Long)
                    out.writeLong((Long)message);
            } catch (IOException e) {}
        }
        synchronized (newMessages) {
            messages.clear();
            messages.addAll(newMessages);
            newMessages.clear();
        }
    }
}

public void write(Object message) {
    synchronized (newMessages) {
        newMessages.add(message);
        interrupt();
    }
}

But this may cause the thread to go to sleep when there are messages to send, for example when the write() method is called just after the run() method made the isEmpty() check, that returned true, but has not yet started to sleep. I really can’t think of a way to avoid this problem with sleep(0), has anyone got a idea? Or am I going the wrong way about this?

Thanks a bunch.

  • 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-16T01:00:29+00:00Added an answer on June 16, 2026 at 1:00 am

    Have a look at LinkedBlockingQueues. You could use a single one of those instead of the messages and newMessages objects in your code.

    This class lets you add items from one thread A and read from another thread B. Thread B will wait until new messages are added by thread A. Should be exactly what you need.

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

Sidebar

Related Questions

I'm building a multiplayer card game using Flex on the client side and Java
A friend and me are working on a Java Game with a client/server -
I wrote a peer-to-peer networked game in Java using the java.net package and for
I'm trying to write multiplayer game for Android with java back-end using sockets, but
I'm going to be porting a java OpenGL game engine that I wrote in
I recently wrote a Java playing card game and have all the game logic
While writing a game for J2ME we ran into an issue using java.lang.Integer.parseInt() We
I'm looking to write a small client-server-based text game that handles multiple client connections
I am trying to make a client/server turn based game. I want this to
BACKGROUND I'm writing a little game using Java, slick2d and other frameworks. Slick2d does

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.