日期:2014-05-20 浏览次数:20649 次
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package xml.main;
import xml.bean.RequestBean;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import xml.common.ParentxmlHandler;
import xml.dao.XmlDao;
/**
*
* @author Administrator
*/
public class xmlMultiThreadServer {
private ExecutorService executorService;// 线程池
private static List<Future> pageFutureList;
private final int POOL_SIZE_ONECPU = 10;// 单个CPU线程池大小
private int POOL_SIZE = 0;
public xmlMultiThreadServer() {
POOL_SIZE = Runtime.getRuntime().availableProcessors()
* POOL_SIZE_ONECPU;
pageFutureList = new ArrayList<Future>();
executorService = Executors.newCachedThreadPool();// 工厂类创建固定线程数(CPU核心数*每个U线程池大小)的线程池
System.out.println("startup StartWorkServer!");
}
public void service() {
List<RequestBean> list;
while (true) {
while (!hasFreeThread()) {
try {
Thread.sleep(2000);// 没有可用线程处理需等待2秒钟
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int freeThreadNum = POOL_SIZE - pageFutureList.size();
XmlDao Dao = new XmlDao();
list = Dao.findAllIsNoOper(freeThreadNum);
System.out.println(list.size());
while (list.size() <= 0) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
list = Dao.findAllIsNoOper(freeThreadNum);
}
for (RequestBean requestBean : list) {
ParentxmlHandler ParentxmlHandler = null;
Class<?> handler = null;
try {
XmlDao dao = new XmlDao();
String sendtype = dao.findsendtype();
if(sendtype.equals("1")){
handler = Class.forName("xml.handler.ExportSaleDataMainHandler");
}
if(sendtype.equals("2")){
handler = Class.forName("xml.handler.ExportSaleDataMonMainHandler");
}
if(sendtype.equals("3")){
handler = Class.forName("xml.handler.ExportKcDataMainHandler");
}
if(sendtype.equals("4")){
handler = Class.forName("xml.handler.ExportShopInfoHandler");
}
if(sendtype.equals("5")){
handler = Class.forName("xml.handler.ExportShopInfoHandler");
}
if(sendtype.equals("6")){
handler = Class.forName("xml.handler.ExportShopInfoHandler");
}
if(sendtype.equals("7")){
handler = Class.forName("xml.handler.ExportShopInfoHandler");
}
if(sendtype.equals("8")){
handler = Class.forName("xml.handler.ExportShopInfoHandler");
}
System.out.println("handler" + handler);
} catch (Exception e) {
e.printStackTrace();
}