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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:02:52+00:00 2026-06-05T05:02:52+00:00

I am trying to create a continuous thread where a server recieves/sends messages from

  • 0

I am trying to create a continuous thread where a server recieves/sends messages from a client however when I try to check for a next element it gets stuck:

public void run()
{
    try
    {
        try
        {
            ArrayList<Socket> connections = parent.getConnections();
            in = new Scanner(socket.getInputStream());

            while(true)
            {
                if(in.hasNextLine()) // Gets stuck here
                {
                    String message = in.nextLine();
                    System.out.println("Client said " + message);
                }
            }
        }

        finally
        {
            socket.close();
        }
    }

    catch(Exception e)
    {
        e.printStackTrace();
    }

How do I make the loop not get stuck at the specified point

  • 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-05T05:02:54+00:00Added an answer on June 5, 2026 at 5:02 am

    Assuming you want to be able to deal with ‘lines’, I’d probably start with something like this:

    public class SocketReader implements Runnable {
    
        private final InputStream stream;
        private final Queue<String> destination;
        private volatile boolean active = true;
    
        private SocketReader(InputStream stream, Queue<String> destination) {
            this.stream = stream;
            this.destination = destination;
        }
    
        public static SocketReader getReader(Socket toRead, Queue<String> destination) throws IOException {
            return new SocketReader(toRead.getInputStream(), destination);
        }
    
        public void shutdown() {
            active = false;
        }
    
        public void run() {
            while(active) {
                if (stream.hasNextLine() && active) {
                    final String line = stream.nextLine;
                    destination.add(line);
                }
            }
            try {
                stream.close();
            } catch (IOException e) {
                // Log somewhere
            }
        }
    }
    

    Drop this into its own thread (or as part of a thread or executor pool, really), and you’ve made the rest of your application non-blocking with regards to this code. EXPECT this to block while waiting for updates from stream.hasNextLine(). You can even supply a BlockingQueue if you don’t wish to actively poll a queue, but are handling updates in some other fashion.

    You can then do something like this for output:

    public class QueuedPrinter implements Runnable {
    
        private final Queue<String> input;
        private final PrintStream destination;
        private volatile boolean active;
    
        public QueuedPrinter(Queue<String> input, PrintStream destination) {
            this.input = input;
            this.destination = destination;
        }
    
        public void shutdown() {
            active = false;
        }
    
        public void run() {
            while(active) {
                final String line = input.poll();
                if (line != null && active) {
                    destination.println(line);
                }
            }
        }
    
    }
    

    Please note that I haven’t tested this, and you may have to adjust things slightly for other Checked exceptions. You probably need to put in additional error-checking code (null-handling comes to mind). Also, this isn’t completely threadsafe, but is likely to be ‘good enough’ for most uses.

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

Sidebar

Related Questions

I am trying to create a thread which will continuously check for changes to
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
Trying to create a macro which can be used for print debug messages when
I'm trying to implement a server-client socket program in Java that can support multiple
I'm trying to accomplish the following: Have a thread that reads data from a
I was trying to create 2D array in a continuous memory block, but it
Using SQL Server, I'm trying to query a kind of averaged count from a
I am trying to get my continuous integration to create build the smart device
Im trying to create a continuous slider image, that loops itself, I have succeeded
I'm trying to create a loop than continues to take input until the input

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.