📄 testsend.java
字号:
synchronized public String GetThreadName(int i){ return nameAry[i]; } synchronized public boolean ApplyNewThread(){ boolean ret = false; if (AllNum<MaxNum){ ret = true; } return ret; } synchronized public void CouldRun(int i){ while (runningNum>=CouldRunning){ try{ wait(); }catch(Exception ea) { System.out.println("异常."); } } runningNum = runningNum+1; readyNum = readyNum-1; status[i] = 2;//写成Running状态 } synchronized public int GetDeadNum(){ int ret = -1; if (DeadNum>0) { for (int j=0;j<MaxNum;j++) { if (status[j]==0) { ret = j; break; } } } return ret; }}/*here is class of ReadQueueThread code*/class ReadQueueThreada extends Thread { CMPPAPI herepCMPPAPI = null; Queue hereQueue; String hereStr = new String(); ResControlCenter hereMonitor = null; LogToFile hereLogFile = null; LogToFile hereReceiveLog = null; String hereName; Object output = null; int myNum; String msg = new String(); byte needReply = 0; byte msgLevel = 1; byte msgFormat = 0x0f; String sendMsg = ""; int msgLength; String ZSWD_SERVICEID="ZSWD"; String ZSWD_SRCID="025016"; String herePhone= new String(); String feeType = "01"; String feeCode = "00"; byte[] sMsgID = new byte[21]; int countnum=0; int baifeng=0; int allnum= 0; int tmpaa=0; CMPPDeliverResp lpDeliverResp = null; public ReadQueueThreada(Queue inQueue,ResControlCenter inMonitor,int i,LogToFile inLogFile,LogToFile ReceiveSMSLog) { herepCMPPAPI = new CMPPAPI(); myNum=i; hereMonitor = inMonitor; hereMonitor.AddReadyNum(i); hereName = hereMonitor.GetThreadName(myNum); hereQueue = inQueue; hereLogFile=inLogFile; hereReceiveLog=ReceiveSMSLog; hereLogFile.DoLog("线程"+hereName+i+"启动\n初始化成功,队列长度是"+hereQueue.GetLength()); // initiate API }//end of 构造函数 public void run() { //线程的执行方法 int j=0; try { while(true) { try { hereMonitor.CouldRun(myNum); //hereLogFile.DoLog( "Henry log: ReadQueueThreada is running." ); //hereLogFile.DoLog("线程"+hereName+"开始工作。现共有"+hereMonitor.GetAllNum()+"个线程,其中READY的是"+hereMonitor.GetReadyNum()+"个,运行的是"+hereMonitor.GetRunningNum()+"个,队列的长度是"+hereQueue.GetLength()); if (!hereQueue.empty()) { output = hereQueue.deq(); msg = (String) output; //herePhone = "13976485124"; herePhone = "13621673888"; byte[] sServiceID = new byte[10]; System.arraycopy( ZSWD_SERVICEID.getBytes(), 0, sServiceID, 0, ZSWD_SERVICEID.length() ); byte[] sFeeType = new byte[2]; System.arraycopy( this.feeType.getBytes(), 0, sFeeType, 0, 2 ); byte[] sFeeCode = new byte[6]; System.arraycopy( this.feeCode.getBytes(), 0, sFeeCode, 0, feeCode.length() ); byte[] sSrcTermID = new byte[21]; System.arraycopy( ZSWD_SRCID.getBytes(), 0, sSrcTermID, 0, ZSWD_SRCID.length() ); byte[] sDestTermID = new byte[21]; System.arraycopy( herePhone.getBytes(), 0, sDestTermID, 0, herePhone.length() ); msgLength=msg.length(); if (msgLength>140) { msgLength=140; } byte[] sMsgCon = new byte[msgLength]; System.arraycopy( msg.getBytes(), 0, sMsgCon, 0, msgLength ); byte[] ba = new byte[17]; hereLogFile.DoLog( "Henry log: ReadQueueThreada before SendSingle, myNum:"+myNum+", thread "+hereName +" start working, sending "+msg ); System.out.println( "Henry log: ReadQueueThreada before SendSingle, myNum:"+myNum+", thread "+hereName +" start working, sending "+msg ); if ( myNum % 3 != 0 ) { if( herepCMPPAPI.CMPPSendSingle( needReply, msgLevel,sServiceID, msgFormat,sFeeType, sFeeCode,ba, ba,sSrcTermID, sDestTermID, msgLength, sMsgCon,sMsgID, (byte)0, null, (byte)0, (byte)0,(byte)0,(byte)0,null ) != 0 ) { System.out.println( "ReadQueueThreada: myNum="+ myNum + ". Fail to call CMPPSendSingle, error=" + herepCMPPAPI.GetErrCode() ); hereReceiveLog.DoLog("线程"+hereName+"发送"+msg+"失败"); //System.exit( 1 ); } else { //System.out.println("Send is OVER!"); hereLogFile.DoLog("; "+countnum+" ;守护线程发送"+msg+"成功"); }//end of if for send sms } else { if( herepCMPPAPI.CMPPDeliver( 5, lpDeliverResp ) != 0 ) { System.out.println( "ReadQueueThreada: myNum="+ myNum + ". Fail to call CMPPDeliver, error=" + herepCMPPAPI.GetErrCode() ); hereReceiveLog.DoLog("Thread "+hereName+"receive failed"); } else { hereLogFile.DoLog("; "+countnum+" ;daemon thread succeed to receive msg."); } } hereLogFile.DoLog("线程"+hereName+"开始一次工作"+msg+"完成。现共有"+hereMonitor.GetAllNum()+"个线程,其中READY的是"+hereMonitor.GetReadyNum()+"个,运行的是"+hereMonitor.GetRunningNum()+"个,队列长度是"+hereQueue.GetLength()); }//end of if empty //this.sleep(60); try { sleep(60); } catch(Exception ee) { hereLogFile.DoLog(" 睡觉都会报错,切。"); } hereMonitor.SubRunningNum(myNum); }//end of try catch(Exception eb){ //System.out.println(hereName+"遇见了"+hereStr+",出错中。"); //eb.printStackTrace(); hereMonitor.SubRunningNum(myNum); hereReceiveLog.DoLog("警告!!线程"+hereName+"工作"+msg+"出错。现共有"+hereMonitor.GetAllNum()+"个线程,其中READY的是"+hereMonitor.GetReadyNum()+"个,运行的是"+hereMonitor.GetRunningNum()+"个,队列长度是"+hereQueue.GetLength()); }//end of try and catch }//end of while }catch(Exception ebb){ //System.out.println(hereName+"遇见了"+hereStr+",出错中,这个线程已经死亡。"); hereMonitor.AddDeadOne(myNum); hereLogFile.DoLog("严重警告!!线程"+hereName+"死亡。现共有"+hereMonitor.GetAllNum()+"个线程,其中READY的是"+hereMonitor.GetReadyNum()+"个,运行的是"+hereMonitor.GetRunningNum()+"个"); } }//end of run}//end of ReadQueueThread/*here is start of thread ReadFromQueue.java*/class DaemonThread extends Thread { //CMPPAPI hereCMPPAPI = null; Queue hereQueue; LogToFile hereLogFile = null; LogToFile threadLogFile = null; LogToFile ReceiveSMSLog = null; Object output = null; ResControlCenter hereMonitor = null; String logStr = new String(); PrintWriter log; String logFile = new String(); ReadQueueThreada[] myThread; final int MAXQUEUELENGTH = 500; final int MINQUEUELENGTH = 20; public DaemonThread(Queue inQueue) { //hereCMPPAPI = inpCMPPAPI; hereLogFile=new LogToFile("SDaemon"); threadLogFile=new LogToFile("SThreadLog"); ReceiveSMSLog=new LogToFile("RLog"); hereQueue = inQueue; hereMonitor = new ResControlCenter(5,3);//共开启15个线程,可以运行为5个 myThread = new ReadQueueThreada[hereMonitor.GetMaxNum()]; hereLogFile.DoLog("守护线程启动\n初始化成功"); }//end of 构造函数 public void run() { //线程的执行方法 int i=0; int tmpCount=0; System.out.println("Henry log: Daemon thread running."); hereLogFile.DoLog( "Henry log: Daemon thread runs here!" ); while (hereMonitor.ApplyNewThread()) { try { //myThread[i] = new ReadQueueThreada(hereQueue,hereMonitor,i,threadLogFile,ReceiveSMSLog,hereCMPPAPI); myThread[i] = new ReadQueueThreada(hereQueue,hereMonitor,i,threadLogFile,ReceiveSMSLog); myThread[i].start(); hereLogFile.DoLog("守护线程启动"+(i+1)+"个线程,成功!,队列长度是"+hereQueue.GetLength()); i++; } catch(Exception eb) { hereLogFile.DoLog("警告!!守护线程启动到"+i+"个线程,失败!"); } } while (true) { i = hereMonitor.GetDeadNum(); if (i>-1) { try { //myThread[i] = new ReadQueueThreada(hereQueue,hereMonitor,i,threadLogFile,ReceiveSMSLog,hereCMPPAPI); myThread[i] = new ReadQueueThreada(hereQueue,hereMonitor,i,threadLogFile,ReceiveSMSLog); myThread[i].start(); hereLogFile.DoLog("守护线程发现死亡线程"+i+",并且重新启动"+i+"个线程,成功!"); } catch(Exception ea) { hereLogFile.DoLog("警告!!守护线程发现死亡线程"+i+",重新启动出错,失败!"); }//end of try and catch }//end of if i hereLogFile.DoLog("线程状态报告:现共有"+hereMonitor.GetAllNum()+"个线程,其中READY的是"+hereMonitor.GetReadyNum()+"个,运行的是"+hereMonitor.GetRunningNum()+"个,队列长度是"+hereQueue.GetLength()); if (hereQueue.GetLength()>=MAXQUEUELENGTH) { hereLogFile.DoLog("一般警告:队列太长:现共有"+hereMonitor.GetAllNum()+"个线程,其中READY的是"+hereMonitor.GetReadyNum()+"个,运行的是"+hereMonitor.GetRunningNum()+"个,队列长度是"+hereQueue.GetLength()); hereMonitor.AddCouldRunNum(2); hereLogFile.DoLog(" 采取措施,增加两个可以运行线程数量。"); } if (hereQueue.GetLength()<=MAXQUEUELENGTH) { hereLogFile.DoLog("一般警告:队列太短:现共有"+hereMonitor.GetAllNum()+"个线程,其中READY的是"+hereMonitor.GetReadyNum()+"个,运行的是"+hereMonitor.GetRunningNum()+"个,队列长度是"+hereQueue.GetLength()); hereMonitor.SubCouldRunNum(2); hereLogFile.DoLog(" 采取措施,减少两个可以运行线程数量。"); } try { sleep(15000); } catch(Exception ee) { hereLogFile.DoLog(" 睡觉都会报错,切。"); } }//end of while }//end of run}//end of DaemonThread
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -