user1125516
user1125516

Reputation: 250

passing static fields to a thread

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?

Upvotes: 2

Views: 432

Answers (2)

overthink
overthink

Reputation: 24463

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.

Upvotes: 2

Martin James
Martin James

Reputation: 24897

Read the static data into a static 'sharedConfig' object that also has a socket field - you can use that field for the listening socket. When acccpet() returns with a server<> client socket, clone() the 'sharedConfig', shove in the new socket and pass that object to the server<>client worker thread. The thread then gets a copy of the config that it can erad and even modify if it wants to without afecting any other thread or the static config.

Upvotes: 0

Related Questions