HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Thread Pooling"
Vol. 3, Issue 2, p. 34

	

Listing 1: UnconstrainedThreadCreator program creates as many 
background threads as indicated on the command-line.
 
public class UnconstrainedThreadCreator 
  implements Runnable { 
 private int m_id; 
  private static int m_nextId = 1; 

  public static void main(String[] astrArgs) { 
    try { 
      int nThreads = Integer.parseInt(astrArgs[1]); 
      for(int ii=0 ; ii<nThreads ; ii++) 
        new UnconstrainedThreadCreator(); 
      return; 
    } catch (Exception e) { 
      System.err.println(e); 
      e.printStackTrace(System.err); 
    } 
  } 

  public UnconstrainedThreadCreator() { 
    synchronized(getClass()) { 
      m_id = m_nextId++; 
    } 

    (new Thread(this)).start(); 
  } 

  public void run() { 
    Thread self = Thread.currentThread(); 
    try { 
      while(true) { 
        self.sleep(100); 
        System.out.println("Thread " + m_id + " looping..."); 
      } 
    } catch (InterruptedException ie) { 
    } 
  } 
} 
       

Listing 2: Internet server class with unconstrained thread creation; one thread is created per client request. (ClientHandler class omited for brevity.)
 
public class INetServer { 

  public static void main(String[] astrArgs) { 
    try { 
      ServerSocket ss = new ServerSocket(8888); 

      while(true) { 
        Socket s = ss.accept(); 
        Client Handler ch = new ClientHandler( 
            s.getInputStream(), s.getOutputStream()); 
        (new Thread(ch)).start(); 
      } 
    } catch (Exception e) { 
       System.err.println(e); 
       e.printStackTrace(System.err); 
    } 
  } 

} 

Listing 3: ThreadPoolManager class and ManagedThread class.
 
public class ThreadPoolManager { 
  private Vector m_runnableVector; 

  public ThreadPoolManager(int threadPoolSize) { 
    m_runnableVector = new Vector(1, 10); 

    for(int ii=0 ; ii<threadPoolSize ; ii++) 
      new ManagedThread(this); 
  } 

  synchronized void threadWaiting(ManagedThread mt) 
      throws InterruptedException { 
    while(0 == m_runnableVector.size()) 
      wait(); 

    Runnable r = (Runnable)m_runnableVector.elementAt(0); 
    m_runnableVector.removeElementAt(0); 
    mt.startRunnable(r); 
  } 

  public synchronized void start(Runnable r) { 
    m_runnableVector.addElement(r); 
    notify(); 
  } 
} 

class ManagedThread extends Thread { 
  private ThreadPoolManager m_manager; 

  ManagedThread(ThreadPoolManager manager) { 
    m_manager = manager; 
    start(); 
  } 

  public void run() { 
    try { 
      while(true) 
        m_manager.threadWaiting(this); 
    } catch (InterruptedException ie) { 
      // just quit. 
    } 
  } 

  void startRunnable(Runnable r) { 
    try { 
      r.run(); 
    } catch (Exception e) { 
      // Print out exceptions thrown by Runnables, 
      // but return normally to allow thread to 
      // continue handling cliuent requests. 
      System.err.println("Error running " + r); 
      System.err.println(e); 
      e.printStackTrace(System.err); 
    } 
  } 
} 

Listing 4: The Internet server program using thread pooling to handle client requests.
 
public class INetServer { 

  public static void main(String[] astrArgs) { 
    try { 
      ThreadPoolManager tpm = new ThreadPoolManager(100); 
      ServerSocket ss = new ServerSocket(8888); 

      while(true) { 
        Socket s = ss.accept(); 
        Client Handler ch = new ClientHandler( 
            s.getInputStream(), s.getOutputStream()); 
> 
        /* HERE'S THE ONLY DIFFERENCE */ 
        tpm.start(ch); 
      } 
    } catch (Exception e) { 
       System.err.println(e); 
       e.printStackTrace(System.err); 
    } 
  } 

}
 
  
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.