I have a simple question, how to convert this server into multithreded as far as now it is dealing with only one client. Which part should go into run() part :)?
ServerSocket listener = new ServerSocket(9090);
System.out.println("server\n");
try {
while (true) {
Socket socket = listener.accept();
System.out.println(socket+" " + "welcome\n");
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(new Date().toString());
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String answer = input.readLine();
System.out.println(answer);
if("hej".equals(answer)){
System.out.println("Sacrafice accepted");
}
} finally {
socket.close();
}
}
} finally {
listener.close();
}
Serving each connection returned from accept should go into a separate thread, i.e. the following should be moved into the
run()method:Of course, you’ll need to add code that creates a thread. Since thread creation is an expensive task and threads are reusable it’s best to create a number of worker threads in a pool up-front during initialization and then only retrieve worker threads from the pool once you accept a connection and need a thread to service it. As you develop your application you may find that adding some logic to adjust thread pool size at run time based on load is a good idea, but you should probably abstain from this at the current stage and just use a configuration item (like a command line option or a static final) to set the initial thread pool size. You can find thread pool implementations in java.util.concurrent.
If you do that, thread’s
run()method will be very simple waiting in a loop for new tasks and every time it receives a task it should run that task’srun()method. The code above should be put into task’s not thread’srun()method. This way you will separate threads from tasks and hence make sure threads remain reusable. Threads will also need a method to receive tasks and that method should thread-safe. You can use one of the queue implementations from java.util.concurrent to store tasks in your threads between the time they’re passing into a thread for servicing and the time they’re taken out of the queue by thread’srun()method to actually run them.This separation of threads and tasks is yet another case when adding another level of indirection solves an important software engineering problem.