⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pooledconnectionhandler.java

📁 java版源代码,里面包含很多源代码,大家可以看看.
💻 JAVA
字号:
package com.trulytech.mantis.socket;

import java.net.*;
import java.util.*;
import com.trulytech.mantis.system.logWriter;
import com.trulytech.mantis.system.Properties;
import java.lang.reflect.Method;

public class PooledConnectionHandler
    implements Runnable {
  //Socket连接
  protected Socket connection;
  //线程池
  protected static List pool = new LinkedList();
  //线程ID
  private int ID = 0;

  /**
   * 处理方法
   */
  public void handleConnection() {
    logWriter.Debug("Socket Thread " + ID + " Processing...");
    //执行ThreadProcess
    try {
      Class[] cl = new Class[1];
      cl[0] = Class.forName("java.net.Socket");
      Object[] obj = new Object[1];
      obj[0] = this.connection;
      Class c = Class.forName(Properties.SocketProcessClass);
      Method m = c.getDeclaredMethod("doProcess", cl);
      m.invoke(this, obj);
    }
    catch (Exception e) {
      logWriter.Error(e.toString());
    }

  }

  /**
   * 构造函数
   * @param ID int ThreadID号
   * @throws Exception
   */
  public PooledConnectionHandler(int ID) throws Exception {
    this.ID = ID;
    logWriter.Info("Socket Thread " + ID + " has started");

  }

  /**
   * 处理Socket请求
   * @param requestToHandle Socket
   */
  public static void processRequest(Socket requestToHandle) {
    synchronized (pool) {
      //如果并发超过最大请求数,自动退出
     if (pool.size() >= (Properties.MaxWait>0?Properties.MaxWait:1)) {
        try {
          if (!requestToHandle.isClosed())
            requestToHandle.close();
          return;
        }
        catch (Exception e) {}
      }
      pool.add(pool.size(), requestToHandle);
      pool.notifyAll();
    }
  }

  /**
   * 运行
   */
  public void run() {
    while (true) {
      synchronized (pool) {
        while (pool.isEmpty()) {
          try {
            pool.wait();
          }
          catch (InterruptedException e) {
            return;
          }
        }
        connection=(Socket)pool.remove(0);
      }
      handleConnection();

    }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -