addthread.java

来源:互联网 发布:vx是什么社交软件 编辑:程序博客网 时间:2024/05/07 17:31
  1. /**
  2.  * Title:        加入线程
  3.  * Description:
  4.  * @date :       2006-3-3
  5.  * @author :     zhanglei
  6.  * @version :    1.0
  7.  */
  8. package server;
  9. import java.io.*;
  10. import java.util.*;
  11. import java.sql.*;
  12. /**
  13.  *RecvThread  交换信息接收XML文件服务线程
  14.  *@param1     线程启动时间
  15.  *@param2     监控文件路径
  16.  */
  17. class RecvThread
  18.     implements Runnable {
  19.     private DomXML DomXML = MainServer.DomXML;
  20.     static int sumFile = 0//处理文件合计数
  21.     private int threadTime = 0//线程延时时间
  22.     private findfile FindFile = null;
  23.     private int execFlag = 0//执行SQL语句结果标识
  24.     private File doFile = null//备份移动后进行处理的文件
  25.     private File errorFile = null//异常XML文件移动
  26.     private String filename = "";
  27.     private String pathname = "";
  28.     private StringBuffer log = new StringBuffer();
  29.     private JdbcWork RkJw = new JdbcWork();
  30. //    private int xmlListFlag = 0; //接收XML的list文件处理标识 0 未处理 1 处理成功 -1 处理失败
  31.     private String threadNo = ""//线程号
  32.     private ListXML listXml = null;
  33.     public RecvThread(String ThreadNo) {
  34.         threadNo = ThreadNo;
  35.         DomXML.GetBeginItem("receive");
  36.         this.threadTime = GetThreadtime(DomXML.GetItemAttribute(threadNo, "threadtime").trim());
  37.         this.pathname = DomXML.GetItemAttribute(threadNo, "path").trim();
  38.         FindFile = new findfile(pathname);
  39.         listXml = new ListXML();
  40.         RkJw.ConnectDB();
  41.     }
  42.     //执行线程体
  43.     public void run() {
  44.         System.out.println("run");
  45.     }
  46.     //获得线程时间
  47.     public static int GetThreadtime(String timeStr) {
  48.         int threadtime = 1000;
  49.         if (MyFunction.IsNumber(timeStr)) {
  50.             threadtime = Integer.parseInt(timeStr);
  51.         }
  52.         return threadtime;
  53.     }
  54.     public static void main(String args[]) {
  55.         //读入检验XML配置到内存
  56.         try {
  57.             DomXML configXml = new DomXML("config.xml");
  58.             System.out.println("XML配置文件读取成功!");
  59.         }
  60.         catch (Throwable t) {
  61.             System.out.println("检验XML配置文件读取错误!");
  62.         }
  63.         Thread RecvThread = new Thread(new RecvThread("no1"));
  64.         RecvThread.start();
  65.         /*      //建立Jdbc数据库连接
  66.                         JdbcWork JW = new JdbcWork();
  67.                         if(JW.ConnectDB() == null) {
  68.                                 System.out.println("数据库连接失败!");
  69.                                 System.exit(1);
  70.                         }
  71.          */
  72.         //户籍变动处理,执行rk_change_people()
  73. //        Thread TestThread = new Thread(new TestThread(1, ""));
  74. //        TestThread.start();
  75.     }
  76. }
  77. /*
  78.  /**
  79.   *ExecSqlThread  户籍变动处理,执行sql语句线程
  80.   *@param1     线程启动时间
  81.   *@param2     执行的SQL语句
  82.   */
  83. class ExecSqlThread
  84.     implements Runnable {
  85.     private DomXML DomXML = MainServer.DomXML;
  86.     private String threadNo = null;
  87.     private int threadTime = 0//线程延时时间
  88.     private String sqlStr = null;
  89.     private JdbcWork EsJw = new JdbcWork();
  90.     public ExecSqlThread(String ThreadNo) {
  91.         threadNo = ThreadNo;
  92.         DomXML.GetBeginItem("execproc");
  93.         this.threadTime = RecvThread.GetThreadtime(DomXML.GetItemAttribute(threadNo, "threadtime").trim());
  94.         this.sqlStr = DomXML.GetItemAttribute(threadNo, "procname").trim();
  95.         EsJw.ConnectDB();
  96.     }
  97.     public void run() {
  98.         while (true) {
  99.             try {
  100.                 EsJw.PstmtExecSql(sqlStr);
  101.                 EsJw.PstmtCommit();
  102.                 Thread.sleep(threadTime);
  103.             }
  104.             catch (Exception e) {
  105.                 SaveLog.SetInfo(sqlStr + "进程执行异常:" + e, 2);
  106.             }
  107.         }
  108.     }
  109. }
  110. /**
  111.  *TabHkThread  户籍变动处理,执行sql语句线程
  112.  *@param1     线程启动时间
  113.  *@param2     执行的SQL语句
  114.  */
  115. class GetXmlThread
  116.     implements Runnable {
  117.     private DomXML DomXML = MainServer.DomXML;
  118.     private String threadNo = null;
  119.     private int threadTime; //线程延时时间
  120.     private String xmlno;
  121.     public GetXmlThread(String ThreadNo) {
  122.         threadNo = ThreadNo;
  123.         DomXML.GetBeginItem("GetXmlFile");
  124.         this.threadTime = RecvThread.GetThreadtime(DomXML.GetItemAttribute(threadNo, "threadtime").trim());
  125.         this.xmlno = ThreadNo; //DomXML.GetItemAttribute(threadNo, "xmltype").trim();
  126.     }
  127.     public void run() {
  128.         while (true) {
  129.             try {
  130.                 GetXmlFile getXmlFile = new GetXmlFile(xmlno);
  131.                 getXmlFile.Start();
  132.                 Thread.sleep(threadTime);
  133.             }
  134.             catch (Exception e) {
  135.                 SaveLog.SetInfo(xmlno + "生成XML文件异常:" + e, 2);
  136.             }
  137.         }
  138.     }
  139. }
  140. /**
  141.  *MyAddThread   自增加线程
  142.  *@ThreadNo     线程启动序号
  143.  */
  144. class MyAddThread
  145.     implements Runnable {
  146.     private DomXML DomXML = MainServer.DomXML;
  147.     private String threadNo = null;
  148.     private int threadTime; //线程延时时间
  149.     private String xmltype;
  150.     public MyAddThread(String ThreadNo) {
  151.         threadNo = ThreadNo;
  152.         DomXML.GetBeginItem("MyAddThread");
  153.         this.threadTime = RecvThread.GetThreadtime(DomXML.GetItemAttribute(threadNo, "threadtime").trim());
  154.     }
  155.     public void run() {
  156.         while (true) {
  157.             try {
  158.                 new MyThreadClass();
  159.                 Thread.sleep(threadTime);
  160.             }
  161.             catch (Exception e) {
  162.                 SaveLog.SetInfo(xmltype + "MyAddThread线程异常:" + e, 2);
  163.             }
  164.         }
  165.     }
  166. }
  167. //调试检测运行进程数量
  168. class Probe
  169.     extends Thread {
  170.     public Probe() {}
  171.     public void run() {
  172.         while (true) {
  173.             Thread[] x = new Thread[100];
  174.             Thread.enumerate(x);
  175.             for (int i = 0; i < 100; i++) {
  176.                 Thread t = x[i];
  177.                 if (t == null) {
  178.                     break;
  179.                 }
  180.                 else {
  181.                     System.out.println(t.getName() + "/t" + t.getPriority()
  182.                                        + "/t" + t.isAlive() + "/t" + t.isDaemon());
  183.                 }
  184.             }
  185.         }
  186.     }
  187. }