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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:03:08+00:00 2026-05-23T00:03:08+00:00

I have a server in Java which is multithreading, and I’ve created a thread

  • 0

I have a server in Java which is multithreading, and I’ve created a thread pool for it.
Now everything goes well and my server accepts and reads data from the clients that connect to it, but I don’t know really how to clean up the sockets after the connections are closed.

So here is my code:

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    ThreadPooledServer server = new ThreadPooledServer(queue,7001);
    new Thread(server).start();
}

ThreadPooledServer class:

public class ThreadPooledServer implements Runnable {
    protected ExecutorService threadPool = Executors.newFixedThreadPool(5);

public ThreadPooledServer(BlockingQueue queue,int port) {
    this.serverPort = port;
    this.queue=queue;
}

    public void run() {
        openServerSocket();

        while (!isStopped()) {
            Socket clientSocket = null;
            try {
                clientSocket = serverSocket.accept();
                clientconnection++;
            } catch (IOException e) {
                if (isStopped()) {
                    System.out.println("Server Stopped.");
        return;
            }

            throw new RuntimeException("Error accepting client connection", e);
        }

        WorkerRunnable workerRunnable = new WorkerRunnable(queue,clientSocket);

        this.threadPool.execute(workerRunnable);

    }
    this.threadPool.shutdown();

    System.out.println("Server Stopped.");
}

private synchronized boolean isStopped() {
    return this.isStopped;
}

public synchronized void stop() {
    this.isStopped = true;

    try {
        this.serverSocket.close();
    }

    catch (IOException e) {
        throw new RuntimeException("Error closing server", e);
    }
}

Here’s what I don’t understand: My while() loop that accepts for clients works as loong as isStopped is false.

When isStopped is set to true, my loop ends and then I shut down my thread pool, which is correct.

isStopped is set to true in onstop(){…………..}….

Where should I call onstop()…?

Because in this moment I’m not using this method ,I’m not calling it and that means that I’m not cleaning correctly my threads.

WorkerRunnable class:

    public class WorkerRunnable implements Runnable {
        public WorkerRunnable(BlockingQueue queue2, Socket clientSocket2) {
        // TODO Auto-generated constructor stub

            this.clientSocket2 = clientSocket2;

            this.queue2 = queue2;
        }

        public void run() {
            try {
                is = new ObjectInputStream(this.clientSocket2.getInputStream());

                try {
                    while (!stop) {
                        System.out.println("Suntem in interiorul buclei while:!");

                        v = (Coordinate) is.readObject();

                        queue2.put(v);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    is.close();

                    clientSocket2.close();
                }

                is.close();
                clientSocket2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void stop() {
            this.stop = true;
        }
    }
}

Here I have the same issue. How should I clean and close up my sockets correctly?

  • 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-23T00:03:09+00:00Added an answer on May 23, 2026 at 12:03 am

    Once you call shutdown() the thread pool will close off each thread once all the tasks are complete.

    You should call onstop() from whatever code knows the pool should be shutdown. This depends on what the rest of your application does and why you would stop the pool before the application has finished.

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

Sidebar

Related Questions

I have created a TCP server-client application in java which exchange periodic messages between
I have a java servlet which accepts an image a user uploads to my
I have a java program which has a server-socket listening infinitely. In case of
Assume I have a java server which uses corba. When I start it with
I have a game server which is made in Java. I want to make
I have a client-server java application, where communication happens over SSL. Right now I
I have an application server in Java which requires parsing a CSV file. Is
I have a server java project, which runs on glassfish. How does it put
I have a Java server page which should execute showPeopleDetails function, when a user
I have a server written in Java that runs as a Windows service (thanks

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.