Threads and processes
Every time you run a program in your computer, it will create a process. This process will know about the execution of your program, that will happen in sequential order, meaning that it will wait for any calls to be completed or instructions to be finished to continue with the execution and it will exist until the program ends (it finishes or it has been closed).
If your program needs to handle multiple calls and wait for responses each time, you might consider using threads in your application.
EXAMPLE
For instance, I have been building an echo server that has a server socket listening for connections. Every time a client sends a request, a connection is created and the process deals with only one request until the client has its response.
To deal with multiple clients, and simultaneous connections, we could use threads. This way, every time a client sends a request, a thread will be created (or *assigned with that connection) and it will be the one responsible to deal with the thread that has been assigned to it.
new Thread(() -> {
try {
ServerIO si = new ServerIO(new PrintStream(out));
Chatterbox server = new Chatterbox(new Listener(si, ss), si);
server.start();
} catch (IOException e){}
}).start();
When the connection is closed, the thread will become available to deal with another connection.
MAIN DIFFERENCES
The main differences between a process and a thread is that a process can contain a (or multiple) threads, but a process can not contain another process, because processes don’t share memory with other processes whereas threads share memory with other threads of the same process.
CONCLUSION
Using threads to deal with multiple calls for your server is good practice.
It will improve performance and it is better than using multiple processes as communication between processes will be slow as processes have different memory addresses.
*The number of threads used by a server, is configured in the code when creating the thread pool. When the number of active threads is reached, the server will put in a queue any new requests, that will be assigned to a thread when it becomes available.