ServerSocket and Thread

There is a problem with the previous example - it will only interact with one client at a time. Typically, a server runs constantly on a server computer, and clients from all over the Internet may want to use the server at the same time. This example introduces the magic of threads to overcome the previous limitation.

main() sits in an infinite loop listening for connection requests. Each request causes a new Socket object to be created. That object is then passed as a parameter to a new "thread" object when each is created and "launched".

All the previous interaction with the client has now been moved to the "thread" class's run() method. You will recall from the section on threads that telling a thread object to start() only puts the object in the "ready" state. It is the thread scheduler that allocates a thread object to the CPU and calls its run() method.