利用計(jì)時(shí)器實(shí)現(xiàn) 對(duì)象狀態(tài) 的 檢測(cè)
當(dāng)對(duì)象在創(chuàng)建后,每隔一定的時(shí)間需要變化一次狀態(tài),并且該狀態(tài)的變化還要通知到用戶(hù),可以利用Java的計(jì)時(shí)器實(shí)現(xiàn)對(duì)對(duì)象狀態(tài)變化的監(jiān)測(cè)。
舉個(gè)例子,如種植了一顆植物,植物每隔兩分鐘需要變化一次狀態(tài),讓后根據(jù)狀態(tài)的不同通知客戶(hù)端進(jìn)行響應(yīng)的事件處理。
植物生長(zhǎng)狀態(tài) |
生長(zhǎng)時(shí)間 |
客戶(hù)端的處理 |
種子 |
2? 分鐘 |
顯示植物的幼苗狀態(tài) |
幼苗 |
3? 分鐘 |
顯示植物的成年?duì)顟B(tài) |
成熟 |
4? 分鐘 |
顯示植物的成熟狀態(tài) |
收獲 |
5 分鐘 |
提示用戶(hù)收獲該作物 |
該事件處理的流程圖如下:
植物的類(lèi)圖
articleID?: 植物ID?
roleID: 所屬角色I(xiàn)D?
currentState: 植物的當(dāng)前狀態(tài)?
timer: 生長(zhǎng)剩余時(shí)間
Java 中計(jì)時(shí)器的用法:
(1)Timer.schedule(TimerTask?task,Date?time)
安排在制定的時(shí)間執(zhí)行指定的任務(wù)。
(2)Timer.schedule(TimerTask?task,Date?firstTime?,long?period)
安排指定的任務(wù)在指定的時(shí)間開(kāi)始進(jìn)行重復(fù)的固定延遲執(zhí)行.
(3)Timer.schedule(TimerTask?task,long?delay)
安排在指定延遲后執(zhí)行指定的任務(wù).
(4)Timer.schedule(TimerTask?task,long?delay,long?period)
安排指定的任務(wù)從指定的延遲后開(kāi)始進(jìn)行重復(fù)的固定延遲執(zhí)行.
(5)Timer.scheduleAtFixedRate(TimerTask?task,Date?firstTime,long?period)
安排指定的任務(wù)在指定的時(shí)間開(kāi)始進(jìn)行重復(fù)的固定速率執(zhí)行.
(6)Timer.scheduleAtFixedRate(TimerTask?task,long?delay,long?period)
安排指定的任務(wù)在指定的延遲后開(kāi)始進(jìn)行重復(fù)的固定速率執(zhí)行.
實(shí)現(xiàn)的源碼:
import?java.util.HashMap;
import?java.util.LinkedList;
import?java.util.Timer;
import?java.util.TimerTask;
import?org.apache.mina.common.IoSession;
import?com.toplx.mina.bussiness.handler.utils.ClientInforUtils;
public?class?Sacnning?{
public?static?Boolean?running?=?false;
public?static?void?scanningProduct()?{
Timer?time?=?new?Timer();
long?delay?=?50;
long?period?=?100;
//? 開(kāi)始執(zhí)行計(jì)劃任務(wù)
time.schedule(new?TimerTask()?{
//? 檢測(cè)服務(wù)器的控制流程
int?k?=?0;
public?void?run()?{
System.out.println(" 開(kāi)始打印???"?+?k?+?"?次");
k++;
running?=?true;
LinkedList<Product>?list?=?ProductList.productList;
System.out.println("??? 植物生長(zhǎng)的鏈表長(zhǎng)度為??"?+?list.size());
int?size?=?list.size();
for?(int?i?=?0;?i?<?size;?i++)?{
Product?product?=?list.getFirst();
Integer?tempTime?=?product.getTimer();
System.out.println("???tempTime??"?+?tempTime);
//? 改變作物的時(shí)間
if?(tempTime?>?0)?{
tempTime--;
product.setTimer(tempTime);
list.removeFirst();
list.addLast(product);
System.out.println("???? 已經(jīng)減--");
System.out.println("???? 植物的當(dāng)前狀態(tài)??"+product.getCurrentState());
}?else?{
System.out.println("??? 向客戶(hù)端發(fā)送植物生長(zhǎng)狀態(tài)");
Integer?currentState?=?product.getCurrentState();
IoSession?session?=?ClientInforUtils.getSessionByID
.get(product.getRoleID());
System.out.println("--------------------"+product.getRoleID());
System.out.println("--------------------"+session);
switch?(currentState)?{
case?1:
product.setCurrentState(2);
product.setTimer(30);
list.addLast(product);
list.removeFirst();
notifyClient(session,?product.getArticleID(),
product.getCurrentState());
break;
case?2:
product.setCurrentState(3);
product.setTimer(30);
list.addLast(product);
list.removeFirst();
notifyClient(session,?product.getArticleID(),
product.getCurrentState());
break;
case?3:
product.setCurrentState(4);
product.setTimer(30);
list.addLast(product);
list.removeFirst();
notifyClient(session,?product.getArticleID(),
product.getCurrentState());
break;
case?4:
product.setCurrentState(5);
product.setTimer(30);
list.addLast(product);
list.removeFirst();
notifyClient(session,?product.getArticleID(),
product.getCurrentState());
break;
case?5:
product.setCurrentState(6);
product.setTimer(30);
list.addLast(product);
list.removeFirst();
notifyClient(session,?product.getArticleID(),
product.getCurrentState());
break;
case?6:
product.setCurrentState(1);
product.setTimer(30);
list.addLast(product);
list.removeFirst();
notifyClient(session,?product.getArticleID(),
product.getCurrentState());
break;
default:
break;
}
}
}
}
},?delay,?period);
}
public?static?void?notifyClient(IoSession?session,?Integer?articleID,
Integer?state)?{
HashMap<String,?Object>?map;
switch?(state)?{
case?1:
map?=?new?HashMap<String,?Object>();
map.put("Event",?"S_Product_Grow");
map.put("productID",?articleID);
map.put("state",?state);
session.write(map);
break;
case?2:
map?=?new?HashMap<String,?Object>();
map.put("Event",?"S_Product_Grow");
map.put("productID",?articleID);
map.put("state",?state);
System.out.println("??? 客戶(hù)端地址??"+session);
session.write(map);
break;
case?3:
map?=?new?HashMap<String,?Object>();
map.put("Event",?"S_Product_Grow");
map.put("productID",?articleID);
map.put("state",?state);
session.write(map);
break;
case?4:
map?=?new?HashMap<String,?Object>();
map.put("Event",?"S_Product_Grow");
map.put("productID",?articleID);
map.put("state",?state);
session.write(map);
break;
case?5:
map?=?new?HashMap<String,?Object>();
map.put("Event",?"S_Product_Grow");
map.put("productID",?articleID);
map.put("state",?state);
session.write(map);
break;
case?6:
map?=?new?HashMap<String,?Object>();
map.put("Event",?"S_Product_Grow");
map.put("productID",?articleID);
map.put("state",?state);
session.write(map);
break;
default:
break;
}
}
}
package?com.toplx.mina.bussiness.handler.allhandlers.product.utils;
public?class?Product?{
private?Integer?articleID;
private?Integer?roleID;
private?Integer?currentState;
private?Integer?timer;
public?Product(Integer?articleID,?Integer?roleID)?{
this.articleID?=?articleID;
<span s
- 2009-03-04 19:07
- 瀏覽 1727
- 評(píng)論(0)
- 相關(guān)推薦
發(fā)表評(píng)論
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

評(píng)論