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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:54:20+00:00 2026-06-02T02:54:20+00:00

I wrote a small HTTP server in Java and I have a problem passing

  • 0

I wrote a small HTTP server in Java and I have a problem passing static variables (server configuration: port, root, etc.) to the thread that handles requests. I do not want my thread to modify these variables and if it extends the server class, it will also inherit its methods which I don’t want.

I don’t want to use getters for reasons of performance. If I make the static members final, I will have a problem when loading their values from the config file.

here’s an example

class HTTPServer {
static int port; 
static File root;
etc..
....
//must be public
public void launch() throws HTTPServerException {
    loadConfig();
    while (!pool.isShutdown()) {
            ....
        //using some config here
             ...
        try {
    Socket s = ss.accept();
    Worker w = new Worker(s);
    pool.execute(w);
        }catch () {...}
    }
}
private void loadConfig(){ //reading from file};
...
other methods that must be public goes here
}

I also don’t want to have the worker as nested class. It’s in another package…

What do you propose?

  • 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-02T02:54:21+00:00Added an answer on June 2, 2026 at 2:54 am

    You could put your config in a final AtomicReference. Then it can be referenced by your worker and also updated in a thread-safe manner.

    Something like:

    class HTTPServer {
      public static final AtomicReference<ServerConf> config = 
        new AtomicReference(new ServerConf());
    }
    

    Make the new ServerConf class immutable:

    class ServerConf {
      final int port;
      final File root;
      public ServerConf(int port, File root) {
        this.port = port;
        this.root = root;
      } 
    }
    

    Then your worker can get a reference to the current config via HTTPServer.config.get(). Perhaps something like:

    Worker w = new Worker(s, HTTPServer.config.get());
    

    loadConfig() can set new config via something like:

    HTTPServer.config.set(new ServerConf(8080, new File("/foo/bar"));
    

    If it’s not important for all your config to change at the same time, you could skip the ServerConf class and use AtomicInteger for the port setting, and AtomicReference<File> for the root.

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

Sidebar

Related Questions

I wrote a small program, kind of specialized HTTP server in haskell, which is
I have created a small game in Java and I would like to add
The http.get() function inside http.createServer is not responding. I wrote a small snippet to
I have a small SOAP CGI gatewqay C++ program that reads an HTTP SOAP
I'm creating a small server using java.nio , but when trying to stress test
I'm looking for a small and fast library implementing an HTTP server in .NET
I'm trying to code a small http server for later extension in Google's Go
I wrote a small PHP application several months ago that uses the WordPress XMLRPC
I wrote a small PHP application that I'd like to distribute. I'm looking for
We wrote a small Windows class library that implements extension methods for some standard

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.