Manage a pool of threads for clients : Server « Network Protocol « Java






Manage a pool of threads for clients

  
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class PoolServerBase extends Thread {
  protected Socket socket;

  static PoolServerBase head = null;

  protected PoolServerBase next = null;

  static protected boolean init(Class clobj, int threads) {
    try {
      for (int i = 0; i < threads; i++) {
        PoolServerBase thread = (PoolServerBase) clobj.newInstance();
        thread.next = head;
        head = thread;
        thread.start();
      }
    } catch (Exception e) {
      return false;
    }
    return true;
  }

  static synchronized protected void addToList(PoolServerBase me) {
    me.next = head;
    head = me;
  }

  synchronized protected void waitForSignal() {
    try {
      wait();
    } catch (InterruptedException e) {
    }
  }

  public void run() {
    while (true) {
      waitForSignal();
      doClientProcessing();
      addToList(this);
    }
  }

  synchronized protected void handleClient(Socket s) {
    socket = s;
    notify();
  }

  protected void doClientProcessing() {
    try {
      String s = "Server.";
      s += "Thread: " + this.toString();
      socket.getOutputStream().write(s.getBytes());
      sleep(10000); // simulate processing
      s = "Complete";
      socket.getOutputStream().write(s.getBytes());
      socket.close();
    } catch (Exception e) {
      System.out.println(e);
    }
  }

  static synchronized protected boolean listEmpty() {
    return head == null;
  }

  static protected void assignThread(Socket sock) {
    PoolServerBase t = head;
    head = head.next;
    t.socket = sock;
    synchronized (t) {
      t.notify();
    }
  }

  static public void startServer(int port) {
    ServerSocket ssock;
    Socket sock;
    try {
      ssock = new ServerSocket(port);
      while (true) {
        Socket esock = null;
        try {
          esock = ssock.accept();
          while (listEmpty())
            yield();
          assignThread(esock);
        } catch (Exception e) {
          try {
            esock.close();
          } catch (Exception ec) {
          }
        }
      }
    } catch (IOException e) {
    }
  }

  static public void main(String args[]) {
    init(PoolServerBase.class, 3);
    System.out.println("Starting server on port 808");
    startServer(808);
  }
}
           
         
    
  








Related examples in the same category

1.A generic framework for a flexible, multi-threaded server
2.Server allows connections on socket 6123Server allows connections on socket 6123
3.This server displays messages to a single clientThis server displays messages to a single client
4.The client can specify information to control the output of a serverThe client can specify information to control the output of a server
5.A server can use specialized streams to deliver typed dataA server can use specialized streams to deliver typed data
6.Serve entire objects using ObjectOutputStreamServe entire objects using ObjectOutputStream
7.A multithreaded serverA multithreaded server
8.Base class to build multithreaded servers easily
9.Client estimates the speed of the network connection to the server
10.This server retrieves the time using the RFC867 protocol.This server retrieves the time using the RFC867 protocol.
11.Quote Server
12.Logging Server based on SocketServer
13.Client and Server Demo
14.Reflector
15.Simple Http Server