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

📄 iothreadpoolmanager.java

📁 this is a trade sale system realized by java. It can run some easy functions and has a good design p
💻 JAVA
字号:
package trader.nw;
import java.io.*;
import java.net.*;
import java.util.*;
import trader.*;
import trader.db.*;

public class IoThreadPoolManager implements IoStrategy{
  private BrokerModel brokerModel;
  private ArrayList clientIoThreadList;

  /** Creates new IoThreadAllocator */
  public IoThreadPoolManager(BrokerModel broker) {
    this.brokerModel = broker;
    clientIoThreadList = new ArrayList(10);
    this.createNewThread();
  }

  public void ioService(Socket socket) {
    // find an available IoThread object
    System.out.println
      ("IoThreadPoolManager.ioService method called");
    IoThread ioThread;
    boolean idleThreadFound = false;
    for (int i=0; i<clientIoThreadList.size(); i++){
      ioThread = (IoThread) clientIoThreadList.get(i);
      if (ioThread.isIdle()){
        ioThread.setSocket(socket);
        idleThreadFound = true;
        System.out.println
          ("IoThreadPoolManager.ioService found idle IoThread");
        break;
      }
    }
    if (!idleThreadFound) {
      System.out.println
        ("IoThreadPoolManager.ioService all IoThreads busy");
      ioThread = this.createNewThread();
      ioThread.setSocket(socket);
    }
  }
  
  private IoThread createNewThread() {
    System.out.println
      ("IoThreadPoolManager creating New IoThread");
    BrokerServer brokerServer = new BrokerServer(brokerModel);
    // create an IoThread object
    IoThread ioThread = new IoThread(brokerServer);
    ioThread.start();           //start the thread
    clientIoThreadList.add(ioThread); //add thread to list
    try {
      //sleep to give newly created thread chance to run
      Thread.sleep(1000);    
    } catch(Exception e) {
      e.printStackTrace();
    }
    System.out.println("No of IoThreads = " 
      + clientIoThreadList.size());
    return ioThread;
  }

  public static void main(String args[]) {
    String dbHost = "localhost";
    try {
      if (args.length > 0) {
        dbHost = args[0];
      }
      BrokerModel bm = new BrokerModelDbImpl(dbHost);
      IoThreadPoolManager ioPM = new IoThreadPoolManager(bm);
    } catch(Exception e) {
       e.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

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