public void addMsgToSendList(MsgEntity msg) { synchronized (this) { Const.broadCastToActivity("INF", "SEND:" + msg.toString(), null, Const.whichActivity.MAIN); CLog.i(tag, "sendMsgList.add:" + msg.toString()); this.sendMsgList.add(msg); notify(); } }
@Override public void run() { CLog.i(tag, "SocketOutputThread begin work"); while (isStart) { CLog.i(tag, "sendMsgList.size()=" + sendMsgList.size()); // 锁发送list synchronized (sendMsgList) { // 发送消息 for (MsgEntity msg : sendMsgList) { String message = new String(msg.getBytes()); Date currTime = new Date(); CLog.i(tag, "####"); CLog.i(tag, "currTime=" + df.format(currTime)); CLog.i(tag, "msg=" + message); CLog.i(tag, "msg.getSendTime()=" + df.format(msg.getSendTime())); if (msg.getTryTimes() > 0 && msg.isComplete()) { sendMsgList.remove(msg); continue; } if (msg.getSendTime().getTime() <= currTime.getTime()) { msg.trySend(); TCPClient.instance(mLogin) .sendMsg(Const.str2EncryptedByte(msg.toString(), "NAN", mLogin.getName())); msg.setSendDelay(Const.RETRY_SEND_SECOND); // Try send 3 times if (msg.getTryTimes() >= Const.RETRY_SEND_TIMES) { Const.broadCastToActivity( "WARN", null, "Send order Fail:" + message, Const.whichActivity.MAIN); msg.setComplete(); } if (msg.isComplete()) sendMsgList.remove(msg); } } } synchronized (this) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); Const.broadCastToActivity( "ERROR", null, "TCPOutputThread.wait():InterruptedException", Const.whichActivity.MAIN); } // After sending message Thread in waiting } } CLog.i(tag, "SocketOutputThread is over!"); }