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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:58:21+00:00 2026-05-28T03:58:21+00:00

I created a little (single thread) server and put the work-code in a Runnable

  • 0

I created a little (single thread) server and put the “work”-code in a Runnable. This runnable is then executed in a background-thread using Javas Executor Framework:

ExecutorService exec = Executors.newSingleThreadExecutor();
exec.execute(runnable);

So now, I’m searching for a good way to shut this server down. I want to add the possibility to restart the Server without restarting the program, so terminating the whole process is not an option.

What I have found is something like this:

class LifecycleWebServer {
    private final ExecutorService exec = ...;

    public void start() throws IOException {
        ServerSocket socket = new ServerSocket(80);
        while (!exec.isShutdown()) {
            final Socket conn = socket.accept();
            // Do stuff with the connection...
        }
    }

    public void stop() { exec.shutdown(); }
}

The thing that bothers me is, that if the program has reached line 7 (socket.accept()), and I call the stop()-method, the Server will still wait until one last connection was made and then shutdown.

Is there any preferred way to deal with this?

  • 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-28T03:58:22+00:00Added an answer on May 28, 2026 at 3:58 am

    So, it seams that calling close() on the waiting ServerSocket is one way to do this. In that case, the accept()-method will throw a SocketException, which you’ll need to catch.

    Another, maybe clearer way is to use a “Poison Pill”, which is explained in detail here: How to interrupt a BlockingQueue which is blocking on take()?

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

Sidebar

Related Questions

I've just created a little app that programmatically compiles code using the C# Compiler,
I created a little web spider in Python which I'm using to collect URLs.
Hi I have created a little application to move some files around and put
I've created my own little c# compiler using the tutorial on MSDN, and it's
I have a page which contains a jQuery-UI horizontal slider, created using a little
I have a little dialog I created using Resource Editor in VS2005. It's going
I'm developing and testing a little straight-forward client-server application using java (and scala). The
I have created a 3d map using rgl.surface(), mainly following Shane's answer in this
I wrote some code to connect the application to it's database, then I created
Lets say you created a little Java Desktop app with a jar and some

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.