iie
iie

Reputation: 501

Multithreading in java - simple

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();
}

Upvotes: 1

Views: 222

Answers (4)

user1032113
user1032113

Reputation:

Use one thread per each client connection. The basic flow of logic in such a multi-client server is this:

while (true) {
    accept a connection ;
    get a thread from pool to deal with the client ;
}

Upvotes: 2

Adam Zalcman
Adam Zalcman

Reputation: 27233

Serving each connection returned from accept should go into a separate thread, i.e. the following should be moved into the run() method:

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();
}

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's run() method. The code above should be put into task's not thread's run() 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's run() 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.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691755

The part after listener.accept(). Once the main thread has received a connection from a client, let another thread (pooled, preferrably) handle the communication with this client so that the main thread can accept another one.

Use a ThreadPoolExecutor to do this: each time a connection is accepted, construct a new Runnable instance and make it execute by the executor.

Upvotes: 1

Nikolay Grudinin
Nikolay Grudinin

Reputation: 31

create a class serverThread, inherit from Thread, to declare a constructor that takes a socket in the Run () method copy the entire try {} catch {}

google translated

Upvotes: 0

Related Questions