Recipe 24.11 Simplifying Servers Using the Concurrency Utilities (JDK 1.5)


You need to implement a multithreaded server.


Use the JDK 1.5 Thread Pool implementation of the Executor interface.


The java.util.concurrent package includes Executors; an Executor is, of course, a class that can execute code for you. The code to be executed can be the familiar Runnable or a new interface Callable. One common kind of Executor is a " thread pool." The code in Example 24-16 subclasses the main class of the Threaded Web Server from Recipe 24.10 to use a pool of Threads to schedule multiple clients concurrently.

Example 24-16.
import; import java.util.concurrent.*; /**  * HttpConcurrent - Httpd Subclass using java.lang.concurrent  */ public class HttpdConcurrent extends Httpd {      Executor myThreadPool = Executors.newFixedThreadPool(5);      public HttpdConcurrent( ) throws Exception {           super( );      }               public static void main(String[] argv) throws Exception {           System.out.println("DarwinSys JavaWeb Server 0.1 starting...");           HttpdConcurrent w = new HttpdConcurrent( );           if (argv.length == 2 && argv[0].equals("-p")) {               w.startServer(Integer.parseInt(argv[1]));           } else {               w.startServer(HTTP);           }           w.runServer( );      }      public void runServer( ) throws Exception {          while (true) {              final Socket clientSocket = sock.accept( );              myThreadPool.execute(new Runnable( ) {                  public void run( ) {                       new Handler(HttpdConcurrent.this).process(clientSocket);                  }              });           }      } }

You can see this program in action in Figure 24-5.

Figure 24-5. HttpdConcurrent in action

See Also

For details on java.util.concurrent, see the online documentation accompanying the JDK. For background on JSR 166, see Doug Lea's home page at and his JSR 166 page at

I have not discussed several general Threads issues. Scheduling of threads is not necessarily preemptive; it may be cooperative. This means that, on some platforms, the threads mechanism does not interrupt the running thread periodically to give other threads a "fair" chance to get CPU time. Therefore, in order to be portable to all Java platforms, your code must use yield( ) or wait( ) periodically (or some other method that causes the thread to be suspended, such as reading or writing). I also didn't get into priorities. The priority model is more limited than that of some other thread models, such as POSIX threads.

All in all, it's important to understand that threaded classes require careful design. For this reason, you should refer to a good book on threaded Java before unleashing anything threaded upon the world. Recommendations include Concurrent Programming in Java by Doug Lea (Addison Wesley), Multithreaded Programming with Java Technology by Lewis et al (Prentice Hall), and Java Threads by Scott Oaks and Henry Wong (O'Reilly).

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: