我编写了以下代码来实现线程服务器:
ServerSocket passiveSocket = new ServerSocket(port, maxConnections);
while(true){
final Socket socket = passiveSocket.accept();
new Thread(new Runnable() {
public void run() {
//access socket as needed to communicate. E.g.:
PrintWriter writer = new PrintWriter(socket.getOutputStream());
//Finally close socket.
socket.close();
}
}).start();
}
现在这似乎可行,但经过反思,我并不真正了解下一个连接到达时最终套接字变量套接字发生了什么。每个线程如何与启动线程时当前的套接字实例相关联?- final关键字对此负责吗?