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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:20:04+00:00 2026-05-22T21:20:04+00:00

I have an app in java which is playing the rolle of a server

  • 0

I have an app in java which is playing the rolle of a server .For limiting the number of incoming connections I’m using a ThreadPool server.

But I have a few problems understanding a part of the code:

Here is y code:


 protected ExecutorService threadPool =
        Executors.newFixedThreadPool(5);


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

}

while (!isStopped()) {

        Socket clientSocket = null;
        try {
            System.out.println("Serverul asteapta clienti spre conectare la port" +serverPort);
            clientSocket = serverSocket.accept();
            clientconnection++;
            System.out.println("Serverul a acceptat clientul cu numarul:"
                    + 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);

    }

}

private void openServerSocket() {

    try {

        InetSocketAddress serverAddr = new InetSocketAddress(SERVERIP,
                serverPort);

        serverSocket = new ServerSocket();


        serverSocket.bind(serverAddr);

    } catch (IOException e) {

        throw new RuntimeException("Cannot open port", e);

    }

}

WHAT I don’t understand:

I’m using a ThreadPooledServer which accepts for 5 incoming connections….

The connection with the clients is done in a while() loop.

while (!isStopped()) {

}

isStopped is a boolean variable returned byt this function:

private synchronized boolean isStopped() {

    return this.isStopped;

}

which I call as a condition for starting the loop.

This boolean variable is initially set to false…..and is set back to true in the here:

public synchronized void stop() {

        this.isStopped = true;

}

When is setup back to true my while() loop ends and then I close up all the workers of my thread pool.

this.threadPool.shutdown();

The problem is that I never call for this function ” stop() “

Question: Is the function called automatically when I close my server?????…or I should call for it somewhere????

  • 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-22T21:20:05+00:00Added an answer on May 22, 2026 at 9:20 pm

    You need to call it somewhere in your code to stop your server and close those connections. If you don’t the system will eventually reclaim its resources as the server will be shutting down.

    You should be able to register a shutdown hook in the JVM (which can call stop()) to help with reclaiming those yourself… Good luck!

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

Sidebar

Related Questions

I have an app in java that is nothing but a remote server.The remote
I have a java app which runs just fine (on Ubuntu 10.04) for few
I have a Swing runnable app which updates messages, then I have a Java
I want to have a java app running, using a function/method (with as little
I have a app engine app (java) which renders a page. It fetches data
I have a Java app which reads CSV files which have been created in
I have a Java app which needs to interact with the camera on a
Let's say I have a Java app which uses a (static) int constant from
i am making a app which takes photo on button click i have camera.java
We have a java app which we call with a parameter (a selected folder),

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.