単純なサーバーの典型的な例:
class ThreadPerTaskSocketServer {
public static void main(String[] args) throws IOException {
ServerSocket socket = new ServerSocket(80);
while (true) {
final Socket connection = socket.accept();
Runnable task = new Runnable() {
public void run() {
handleRequest(connection);
}
};
new Thread(task).start();
}
}
}
なぜSocketとして宣言する必要がありますfinalか?Threadリクエストを処理するnewがsocketメソッド内の変数を参照し直して、ある種の原因となる可能性があるためConcurrentModificationExceptionですか?