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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:54:10+00:00 2026-05-27T18:54:10+00:00

Hi could somebody help me with this code because I was trying with If

  • 0

Hi could somebody help me with this code because I was trying with If statement but is no working. I am not asking to write for me code just point me at something.

The main problem is that every time somebody connect, new thread is made but when he turn off his chat box the tread stays on and is not working as should be. I mean some body make 10 connections himself and nobody else can chat.

import java.net.*;
import java.io.*;

public class myServer {

    static ServerSocket server;
    static Socket client;
    static DataInputStream in;
    static DataOutputStream out;
    static clientThread t[] = new clientThread[10];

    public static void main(String[] args) throws IOException {

        System.out.println("Starting Server");
        server = new ServerSocket(7555);
        System.out.println("Started Server");

        while (true) {

            client = server.accept();
            System.out.println("CONNECTION");
            out = new DataOutputStream(client.getOutputStream());
            out.writeUTF("Welcome to the chat room");
            for (int i = 0; i <= 9; i++) {
                if (t[i] == null) {
                    (t[i] = new clientThread(client, t)).start();
                    break;
                }
            }
        }

    }

}

class clientThread extends Thread {

    DataInputStream in;
    DataOutputStream out;
    static String msg;
    Socket client = null;
    clientThread t[];

    public clientThread(Socket client, clientThread[] t) {
        this.client = client;
        this.t = t;
    }

    public void run() {

        try {
            in = new DataInputStream(client.getInputStream());
            out = new DataOutputStream(client.getOutputStream());
            boolean tru = true;

            while (tru) {
                msg = in.readUTF();
                System.out.println(msg);
                for (int i = 0; i <= 9; i++)
                    if (t[i] != null) {
                        t[i].out.writeUTF(msg);
                        System.out.println(t[i]);
                    }
            }
        } catch (IOException e) {
        }
    }
}
  • 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-05-27T18:54:11+00:00Added an answer on May 27, 2026 at 6:54 pm

    Your problem is not that the thread stays on, but rather that you have no mechanism for marking your client threads as finished. Even though the thread has exited, t[i] will not become null. It will still refer to an instance of a thread–just a “dead” thread.

    Here’s are two ways to fix it:

    1. Just before your thread exits, mark t[i] = null (where i is the index of the current thread). Note that you’ll need to store the value of i in each thread.

      1. Modify clientThread and add private int threadIndex; as a member variable.
      2. Modify clientThread‘s constructor and add threadIndex as a parameter.

        public clientThread(Socket client, clientThread[] t, int threadIndex) 
        {
            this.threadIndex=threadIndex;
            //...
        }
        
      3. Right before the closing brace of run, add

        synchronized(t){t[this.threadIndex]=null;}
        
    2. Use an Executor and submit your clientThreads to it. Java’s Executors will handle cleaning up threads for you.

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

Sidebar

Related Questions

Could somebody please name a few. I could given time, but this is for
I got some problems with my patterns. Hope somebody could help me with this.
Guys, I opened this question (I hope could help some others) but reading stackoverflow
I'd be extremely grateful if somebody could help me, or point me in the
Could somebody please help me get Clang up and running? (I don't have 3.2)
I've run into a little hurdle and wanted to see if somebody could help
I'm a newb when it comes to javascript. Perhaps somebody could help me with
Could somebody please point me in the right direction for learning how to do
Could somebody point me to an example how to search for phrases with Lucene.net?
I am a beginner in jQuery. It would be great if somebody could help

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.