[英]How to stop a countdown “Timer” in Java?
我正在嘗試為自己制作的教育游戲做一個倒數計時器(僅出於學習目的),但是我遇到了一個小問題。
總而言之,我只需要一個計時器:
我的游戲有很多問題需要解決,每個問題都必須在10秒之前解決。 我創建了一個名為“ Chronometer”的類來解決我的游戲中的問題,但是我不知道如何停止它並引發所需的異常(順便說一句,這是聯系我的觀點的最佳方式?)。
當前,它從10計數到0,但是停止,而是繼續計數,下一次從59到0計數-並且永不停止。 我該如何解決?
public class Chronometer {
private Timer chronometer;
private DateFormat format = new SimpleDateFormat("ss");
private Calendar calendar = Calendar.getInstance();
private final byte countType;
public static final byte PROGRESSIVE = 1;
public static final byte REGRESSIVE = -1;
public Chronometer(int years, int months, int days, int hours, int minutes, int seconds, byte countType) {
this.chronometer = new Timer();
calendar.set(years, months, days, hours, minutes, seconds);
this.countType = countType;
}
public void starts_chronometer(){
TimerTask task = new TimerTask() {
public void run() {
System.out.println(getTime());
}
};
chronometer.scheduleAtFixedRate(task, 0, 1000);
this.chronometer = null;
}
public int getTime() {
calendar.add(Calendar.SECOND, countType);
return Integer.parseInt(format.format(calendar.getTime()));
}
}
(失敗,在計時碼表啟動的同時拋出異常。)
public void starts_chronometer() throws Exception {
TimerTask task = new TimerTask() {
public void run() {
System.out.println(getTime());
}
};
chronometer.scheduleAtFixedRate(task, 0, 1000);
this.chronometer = null;
throw new Exception("The time's over!");
}
(失敗,而不是9 ... 8 ... 7 ... 6的工作方式類似於9 ... 6 ... 5 ... 3..1)
public void starts_chronometer() {
TimerTask task = new TimerTask() {
public void run() {
System.out.println(getTime());
if(getTime() == 0){
chronometer.cancel();
}
}
};
chronometer.scheduleAtFixedRate(task, 0, 1000);
this.chronometer = null;
}
我已經在StackOverflow中看到了很多關於計時器的問題,但是沒有一個問題可以幫助我解決問題。
請幫我解決這個問題。
解決此問題的最佳方法是使用觀察者模式。 通過這種模式,您將擁有觀察者和可觀察者。 觀察者可以觀察可觀察對象(因此得名)。 Obervable不在乎是否有觀察者在觀察他。
我提出的解決方案有兩個類,即Main類(Observer)和Chronometer類(Observable)。 Main類將自身作為Observer添加到Chronometer對象,並且當Observerable有需要通知的內容時,將運行void update(Observable, Object)
方法。 天文台不再使用計時器。 相反,它使用的線程將休眠10秒鍾,之后將狀態設置為Modifyed並通知所有Observer,從而調用void update(Observable, Object)
。
通過此實現,計時碼表將在完成后通知每個觀察者。 這將使您知道何時必須更新視圖。
編輯:我已經更改了run
和update
方法的實現,因此每秒都會通知Observer。
主班:
public class Main implements Observer{
public static void main(String[] args) {
Main m = new Main();
Chronometer c = new Chronometer(2014, 7, 4, 13, 46, 21, (byte) 0);
c.addObserver(m);
c.run();
try {
Thread.sleep(200000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void update(Observable arg0, Object arg1) {
int time = (int)arg1;
if(time > 0){
System.out.println("Time left: " + time);
} else {
System.out.println("The time's over!");
//Update View
}
}
}
天文鍾:
public class Chronometer extends Observable implements Runnable{
private DateFormat format = new SimpleDateFormat("ss");
private Calendar calendar = Calendar.getInstance();
private final byte countType;
public static final byte PROGRESSIVE = 1;
public static final byte REGRESSIVE = -1;
public Chronometer(int years, int months, int days, int hours, int minutes, int seconds, byte countType) {
calendar.set(years, months, days, hours, minutes, seconds);
this.countType = countType;
}
public int getTime() {
calendar.add(Calendar.SECOND, countType);
return Integer.parseInt(format.format(calendar.getTime()));
}
@Override
public void run() {
int time = 10;
do {
try {
Thread.sleep(1000);
setChanged();
notifyObservers(time);
time--;
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (time >= 0);
}
}
嘗試以下操作停止:
public class Chronometer {
private Calendar calendar = Calendar.getInstance();
private static final long TIME_0 = -62167489199561L;
private final byte countType;
public static final byte PROGRESSIVE = 1;
public static final byte REGRESSIVE = -1;
public Chronometer(int years, int months, int days, int hours, int minutes, int seconds, byte countType) {
calendar.set(years, months, days, hours, minutes, seconds);
this.countType = countType;
}
public void startChronometer(){
Timer t = new Timer();
TimerTask task = new TimerTask() {
public void run() {
System.out.println(getTime());
if(getTime()==0) throw new RuntimeException("Done");
calendar.add(Calendar.SECOND, countType);
}
};
t.scheduleAtFixedRate(task, 0, 1000);
}
//Returns seconds left
public long getTime() {
return Math.round((calendar.getTime().getTime()-TIME_0)/1000);
}
public static void main(String[] args) {
Chronometer c = new Chronometer(0, 0, 0, 0, 0, 10, REGRESSIVE);
c.startChronometer();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.