?? iothreadpoolmanager.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();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -