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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Network Protocol » Server 




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 = (PoolServerBaseclobj.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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.