因为需求简单,想简单使用下timer来实现一个定时任务,过程中出现了如下问题,有点丈二和尚摸不着头脑,
一直报一下错误,百度了下说是线程已经关闭了,难道是nutz框架自动处理关闭的吗
Source not found for TimerThread.run() line: not available
if(redisService.hget(Constants.RoomPrefix+roomId, Constants.TimeDownField) == null){//还没开始倒计时
redisService.hset(Constants.RoomPrefix+roomId, Constants.TimeDownField, "1");
//开始游戏前的room和user对象的信息更新和第一轮发牌
final Timer timer = new Timer();
TimerTask myTimeTask = new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
action2Round1(roomId, resp, timer);
}
};
long timestamp = 3000;
/**
* myTimeTask 指定执行的线程 date 指定第一次日期执行的时间 timestamp 指定每格多久再次执行一次
*/
timer.schedule(myTimeTask, timestamp);
return true;
}
这是timertask任务的内容,每次执行到这里有报线程已经结束
private void action2Round1(final Integer roomId,final NutMap resp, Timer timer) {
// TODO Auto-generated method stub
final NutMap fp1 = new NutMap();
Trans.exec(new Atom(){
public void run() {
Set<String> openIdsInRoom = redisService.keys(roomId+Constants.UsersPrefix+"*");
Room r = Json.fromJson(Room.class,redisService.hget(Constants.RoomPrefix+roomId, Constants.RoomField));
Set<String> openIdsInGame = r.getOpenIdsInGame();
if(openIdsInRoom != null && openIdsInRoom.size() > 1){
List<Poker> pokerList = poker.createPoker();//初始化一副牌
Poker maxPoker = null;
for(String openId : openIdsInRoom){
User u = Json.fromJson(User.class,redisService.get(openId));
if(u.getUserState() == 1){
//两张底牌
Poker[] pokerAry = new Poker[5];
pokerAry[0] = poker.getPoker(pokerList);
pokerAry[1] = poker.getPoker(pokerList);
resp.setv("action", "backPokers");
resp.setv("msg", pokerAry);
endpoint.sendJson(session.getId(), resp);
//一张面牌
pokerAry[2] = poker.getPoker(pokerList);
if(maxPoker == null || maxPoker.compare(pokerAry[2]) == -1){
maxPoker = pokerAry[2];
redisService.hset(Constants.RoomPrefix+roomId, Constants.BeforeIndexField, String.valueOf(u.getIndex()));
}
u.setPokerAry(pokerAry);//每个玩家自己的扑克牌
u.setUserState(2);//更改游戏状态
openIdsInGame.add(openId);//放入games集合
u.setScore(0);//初始化积分
//更新user的redis缓存
redisService.set(openId, u.toString());
fp1.setv(String.valueOf(u.getIndex()), pokerAry[2]);
}
}
//通知前端开始发牌
resp.setv("action", "frontPoker1");
resp.setv("msg", fp1);
sendAllSync(resp, roomId);
r.setPokerList(pokerList);
redisService.hset(Constants.RoomPrefix+roomId, Constants.RoomField, r.toString());
}else{
resp.setv("action", "noEnoughUser");
resp.setv("msg", "当前在线人数少于2人,无法开始游戏!");
endpoint.sendJsonSync(session.getId(), resp);
}
}
});
}