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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:50:40+00:00 2026-05-18T23:50:40+00:00

Here’s a snippet of a web server that I am currently building… // …

  • 0

Here’s a snippet of a web server that I am currently building…

// …

threadPool = Executors.newCachedThreadPool();
while (true)
  if(this.isOn) {
                try { // listen for incoming connection
              this.clientSocket = serverSocket.accept();
      } catch (IOException e) {
                   System.err.println("LOG: >> Accept failed! ");
                   System.exit(1);
                  }
      // as soon as a connection is established send the socket
      // with a handler/processor to the thread pool for execution

                    threadPool.execute(new ClientRequestProcessor(clientSocket));
        }

//…

Please note that the isOn variable is a VOLATILE boolean.

If I turn the if into a while… this code works… but as it is it doesn’t. May I ask why? From a logical point of view both should work, even if I test that flag in an if… am I missing something?!

[Later edit:] By not working I mean… a browser (e.g. firefox) cannot connect, actually it keeps trying but timesout eventually. Again, if I change that if(isOn) into a while(isOn) it works like a charm.

Any suggestions/ideas are more than welcome!!!

P.S. I need this combo “while(true) if/while(test flag) {…}” because the server can be started/stopped from a GUI… so the top level while(true) is kind of needed so I can recheck whether I am on (and thus listening for connections) or if I am off (and don’t really care about incoming connections). Needles to say the event handlers of the GUI can modify the flag at anytime.

  • 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-18T23:50:40+00:00Added an answer on May 18, 2026 at 11:50 pm

    A better solution is to close the server socket when you want it to stop and start a new one in a new thread when you want it to start. This way you reject new connections and don’t consume CPU when its not doing anything.

    When isOn == true and you turn it false, it will only not accept connections after the next new connection. (Could be any time later) Additionally any client new connections will just wait for the accept to be called (or eventually timeout) You can have up to 50 connections waiting to be accepted by default.

    When isOn == false, your thread will busy wait, consuming a CPU. I suggest you put in a little delay such as Thread.sleep(250). This will cut CPU dramatically, but not delay starting again too much.

    BTW:

    • if you get an exception, you should log/print it out. Otherwise when it fails you won’t know why.
    • If accept fails, it could be the process is out of files, so you doesn’t want it to just die, killing all existing connections.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's what I'm trying to accomplish with this program: a recursive method that checks
Here's a coding problem for those that like this kind of thing. Let's see
Here is the code in a function I'm trying to revise. This example works
Here is what I am currently doing. PHP echo's out the recent post in
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
That's pretty much it. I'm using Nokogiri to scrape a web page what has
here is my configuration: http://domain.com (obviously fictitious name...) hosted on a server running Apache
Here is the css: #content ul { font-size: 12px; } I am trying this:
here is my php code $titikPetaInti = array(); while($row = mysql_fetch_assoc($hasil2)) { $titikPetaInti[] =
Here is the problem that I am trying to solve. I have two folders

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.