Networked Threads
We’ve seen how to make separate threads of control in a Java applet or application, and we’ve discussed the various ways that the Java API allows you to manage threads at runtime. Now we’ll go over some of the issues that arise with multithreaded distributed applications, and how the Java environment helps you deal with them.
Asynchronous Agents
The threaded implementation of our
Solver interface in Example 4.1 shows how multithreaded servers can be implemented in Java. This allows our server to respond to clients asynchronously and to service their requests in parallel, which can reduce the amount of time a client has to wait for a response. The alternative is to have a server with only one thread servicing clients on a first-come, first-serve basis. So if client A is the first client to make a request, the server begins processing it right away. If client B makes a request while the server is processing client A’s job, then B will have to wait for the server to finish A’s job before its job can be started. In fact, client B won’t even get an acknowledgment from the server until client A’s job is done. With the multithreaded server, an independent thread can listen for client requests and acknowledge them almost immediately (or as soon as the thread scheduler gives it a CPU time slice). And with the jobs being allocated to separate threads for processing, the CPU resources will be spread out between the two jobs, and B’s job will potentially finish sooner (though ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access