|
1。前段时间开发了个WAP上聊天室的机器人。流程图如下

2.数据模型

3.Main Source:
RobotBeat.java
package pubchat;
import java.io.*; import java.net.*; import java.util.*; import pubchat.dao.*; import pubchat.client.*; import org.apache.log4j.Logger; import pubchat.wap.ClientTest;
public class RobotBeat extends Thread { Logger log = Logger.getLogger(RobotBeat.class); int timediff; //组件重画的间隔时间 volatile boolean shouldrun; //设为false为停止线程 ChatDAO chatDao = null; RobotDAO robotDao = null; Vector roomVec = null; int roomSize = 0; Vector robotVec = null; boolean clearFlg = false; private static int wordsCount = 0;
public RobotBeat() { this.timediff = 1 * 60 * 1000; this.shouldrun = true; System.out.println("机器人线程启动。。。。。"); this.chatDao = new ChatDAO(true); this.robotDao = new RobotDAO(true); this.roomVec = chatDao.selectChatRoom(); //大类,小类,房间id, 房间名,房间类 this.roomSize = this.roomVec.size(); this.robotInit(); }
public void run() { while (shouldrun) { try { //robotInit();
//robotClear(); //robotInit(); wordsCount++; long start = System.currentTimeMillis(); System.out.println("Sleep start:"+start); sleep(timediff); long end = System.currentTimeMillis(); System.out.println("Sleep End:"+end); //随机说话 // if (this.getProperty()==Driver.FLT_ROBOT) { //机器人线程 if (wordsCount%20==0) { System.gc(); robotClear(); robotInit(); } else { ClientTest content = new ClientTest(); for (int i = 0; i < robotVec.size(); i++) { String sayId = (String) robotVec.elementAt(i); String sayWords = this.robotDao.selectRandomWord(); System.out.println("机器人 " + sayId + ":" + sayWords); content.say(sayId, sayWords); } content = null; }
[1] [2] [3] [4] [5] 下一页 |