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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:18:02+00:00 2026-06-09T13:18:02+00:00

I am trying to create a java socket program with multithread concept. Every single

  • 0

I am trying to create a java socket program with multithread concept.
Every single connection from client to the server is a thread.
Every client send their information to server.

I want to ask how to collect all value from the threads and put them all in an array of string?

Is needed to synchronized all this threads?

What the different if use only Thread t = new Thread() instead of Thread[] t = new Thread(client)?

Am i right for the beginning step with use Thread[] to collect all item from the threads?

Is it useful to use thread join()?

This is part which create thread,

    public void listenSocket(int client){ 
        int i=0;

        Thread[] t = new Thread[client];
        while(i<client){
            ClientWorker w;
              try{  
                    w = new ClientWorker(server.accept());
                    t[i] = new Thread(w);
                    t[i].start();
                    System.out.println(t[i].getName());
              } catch (IOException e) {
                    System.out.println("Accept failed: 4444");
                    System.exit(-1);
              }
        i++;
        }
}

This is the part of what the thread doing

class ClientWorker implements Runnable{
    Socket client;
        String ip;
        String load;
        String data[]=new String[7];


        ClientWorker(Socket client){
        this.client = client;
    }

    public void setIP(String ip){
            this.ip = ip;
        }

        public void setData(String load){
            this.load = load;
        }

        public void getIP(){
           System.out.println(ip);
        }

        public void getData(){
           System.out.println(load);
        }

    public void run(){

        BufferedReader in = null;
        PrintWriter out = null;
                XmlGenerator xml = new XmlGenerator();
        try{
          in = new BufferedReader(new InputStreamReader(client.getInputStream()));
          out = new PrintWriter(client.getOutputStream(), true);
        } catch (IOException e) {
          System.out.println("in or out failed");
        }
                while(true){
                    try{
                        String a = in.readLine();
                        setData(a);
                        String b = client.getRemoteSocketAddress().toString();
                        setIP(b);
                        out.println(a);
                        getData();
                        getIP();
                       }
                    catch (IOException e) {
                        System.out.println("Read failed");
                       }
                }

    }

}`

i’ve read the topic about sync,callable thread but i found nothing.
sorry for repost.

  • 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-09T13:18:03+00:00Added an answer on June 9, 2026 at 1:18 pm

    There are two ways to answer this:

    First way:

    I want to ask how to collect all value from the threads and put them all in an array of string? Is needed to synchronized all this threads?

    If you are going to collect the results that way, then yes it is necessary to explicitly synchronize the threads’ use of that array.

    What the different if use only Thread t = new Thread() instead of Thread[] t = new Thread(client)?

    It is not clear what you mean, but either way if you have two threads accessing / updating the same data structure then they need to synchronize for the application to be reliable / portable.

    Am i right for the beginning step with use Thread[] to collect all item from the threads?

    It is one approach. But there are better approaches.

    Is it useful to use thread join()?

    If you are going to try to address this problem at this level, then it could be useful.

    The problem is that your code doesn’t give me much confidence that you have a clear idea of what you are doing. Or to put it another way, there is no evidence of a design. And it is not really possible to give you specific advice if we can’t figure out how you think your code should work. The code certainly doesn’t seem to line up with the things you are saying at the start of your question …


    The Second way to answer this is that from Java 5 onwards, the “java.util.concurrent” package provides a broad range of “off the shelf” components for implementing multi-threaded systems. What you seem to be trying to implement sounds like a good match for an ExecutorService. It can deal with all of the low-level details of creating and managing threads, and allows the application to get the results of each computation back via a Future. This takes away the need for explicit synchronization … the ExecutorService and Future implementations take care of that.

    So, my recommendation would be to read the Javadocs for ExecutorService, etcetera and figure out how to do what you are trying to do using that. Your code is likely to be better if you do it that way.

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

Sidebar

Related Questions

I'm trying to implement a server-client socket program in Java that can support multiple
I am trying to create an SSL Socket Server/Client between .NET and Java. In
I am trying to create a thread to simply send the text to client.
I want to create http socket connection for server in php for multiple client
I'm trying to create a DHCP Client using Java. The client will send Discover,
I'm trying to send an xml message to a server from a client app
I am trying to write a multithread program in Java where a server listens
I'm trying to create a PHP client to connect to a simple java server,
I'm trying to send messages (byte arrays) from Node.js to Java via TCP socket
Am trying to create a java project which can able to send push notification

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.