我用 Java 编写了一个小型 HTTP 服务器,但在将静态变量(服务器配置:端口、根等)传递给处理请求的线程时遇到问题。我不希望我的线程修改这些变量,如果它扩展了服务器类,它也会继承我不想要的方法。
出于性能原因,我不想使用吸气剂。如果我将静态成员设为最终成员,则在从配置文件加载它们的值时会遇到问题。
这是一个例子
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
}
我也不想让工人作为嵌套类。它在另一个包裹里...
你有什么建议?