私はループでタイマーを開始するスタートボタンを持っています。ボタンでタイマーを停止する
public synchronized void startTourButtonActionPerformed(java.awt.event.ActionEvent evt) {
new java.util.Timer().schedule(new java.util.TimerTask() {
@Override
public void run() {
try {
byte st = presetNo[count];
System.out.println("Start Tour Button pressed, String: " + st);
// this part will run from 0 to max of presets every
// counting.
count++;
if (count >= MaxCount)
count = 0;
byte[] command = { (byte) startTx, address, byteOne, goPreset, 0x00, st, endTx, 0x0F };
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(command);
}
catch (IOException e) {
e.printStackTrace();
}
}
}, 100, 5000);
}
もう1つの「停止」ボタンがあります。現在私はそれを壊して例外を取得することによってループを停止しています。明らかに正しい手順ではなく、開始ボタンも機能しなくなります。
public synchronized void stopTourButtonActionPerformed(java.awt.event.ActionEvent evt){
count = -1;
}
私の質問は、後で再開できるように正しく停止する方法です。
[Javaでタイマーを停止する方法]の可能な複製(http://stackoverflow.com/questions/33726952/how-to-stop-a-timer-in-java) – karan