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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:05:33+00:00 2026-06-03T14:05:33+00:00

So, my server app causing a huge cpu usage averaging about 95%. I think

  • 0

So, my server app causing a huge cpu usage averaging about 95%. I think the reason is it keep spawning new thread but not closing it. how am I suppose to close a thread when any of this happen: refresh page, logout, browser closed.

My code is more or less like this for server part.

ThreadedEchoServer.java

public class ThreadedEchoServer {
    // using port 2000
    static final int PORT = 2000;

    public static void main(String args[]) {
        ServerSocket serverSocket = null;
        Socket socket = null;

        try {
            serverSocket = new ServerSocket(PORT);
        } catch (IOException e) {
            e.printStackTrace();

        }
        while (true) {
            try {
                socket = serverSocket.accept();
            } catch (IOException e) {
                System.out.println("I/O error: " + e);
            }

            // new thread for a client
            new EchoThread(socket).start();
        }
    }
}

EchoThread.java

/*
Class for java server to accept incoming stream
and create a new thread to save log file data in server
*/
public class EchoThread extends Thread {
    protected Socket socket;

    public EchoThread(Socket clientSocket) {
        this.socket = clientSocket;
    }

    public void run() {
        /*Create a File*/
        int i = 1;
        try {
            File directory = new File("F:/temp/tmplog/" + getDate());
            if(!directory.exists()) {
                directory.mkdir();
            }

            String fileName = "F:/temp/tmplog/" + getDate() + "/" + getDateTime() + ".txt";
            File file = new File(fileName);

            //Double Make Sure it create the file
            while(file.exists()) {
                file = new File(fileName + "." + i);
                i++;
            }
            FileOutputStream fis = new FileOutputStream(file, true);
            PrintStream out = new PrintStream(fis);
            System.setOut(out);


            while (true) {
              try {
                InputStream is = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is, "US-ASCII"));

                String line = null;

                while ((line = br.readLine()) != null) {
                  System.out.println(line);
                }
              } catch (IOException exception) {
                // Just handle next request.
              } finally {
                if (socket != null) {
                  try {
                    socket.close();
                  } catch (IOException ignored) {
                  }
                }
                fis.close();
              }
            }


        } catch (IOException ignored) {
        }
    }

This server app basicly will open a new and write a log file for every thread/client. I think the problem is I do not close the thread after use. that’s why it just keep spawning new thread.. Any help?

  • 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-03T14:05:34+00:00Added an answer on June 3, 2026 at 2:05 pm

    If I understood what you’re looking for, you can just use a timeout to handle this situation.
    When the timeout expires, you’ll just terminate the thread.

    EchoThread

    /*
    Class for java server to accept incoming stream
    and create a new thread to save log file data in server
    */
    public class EchoThread extends Thread {
        protected Socket socket;
    
        public EchoThread(Socket clientSocket) {
            this.socket = clientSocket;
            this.socket.setSoTimeout(10000); //Sets timeout to 10 seconds
        }
    
        public void run() {
            /*Create a File*/
            int i = 1;
            try {
                File directory = new File("F:/temp/tmplog/" + getDate());
                if(!directory.exists()) {
                    directory.mkdir();
                }
    
                String fileName = "F:/temp/tmplog/" + getDate() + "/" + getDateTime() + ".txt";
                File file = new File(fileName);
    
                //Double Make Sure it create the file
                while(file.exists()) {
                    file = new File(fileName + "." + i);
                    i++;
                }
                FileOutputStream fis = new FileOutputStream(file, true);
                PrintStream out = new PrintStream(fis);
                System.setOut(out);
    
    
                while (true) {
                  try {
                    InputStream is = socket.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is, "US-ASCII"));
    
                    String line = null;
    
                    while ((line = br.readLine()) != null) {
                      System.out.println(line);
                    }
                  } catch (IOException exception) {
                    // Just handle next request.
                  } finally {
                    if (socket != null) {
                      try {
                        socket.close();
                      } catch (IOException ignored) {
                      }
                    }
                    fis.close();
                  }
                }
    
    
            } catch (IOException ignored) {
            } catch (SocketException e) { //Exception thrown by timeout
                socket.close(); //We close the socket
                this.stop(); //We stop the thread
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have client-server app, I'm managing connections with Threads and handlers inside app, but
I'm running a windows c++ multithreaded app in which one instance/thread of the server
I'm having a very strange problem. My app is causing my server to go
I have client-server app. Client on C++, server on Java. I am sending byte-stream
I have a client app and a server app. The client calls a wcf
I have an ASP.NET MVC server app and client code which uses jquery. I'd
I have a simple client-server app on android. the android service communicates with the
I'm writing a client-server app, in which the client has a determined memory address
I'm developing a app that requires sync with the server app every 5 minutes...
I'm programming a client server app. I'm wondering to end the connection, which side

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.