Моя цель - запустить программу в java, которая выполняет список кода в определенное время каждый день.Обновление Calendar.SECOND in while loop
Я знаю утилиты TimerTask и Timer, но есть причина не использовать их. Большая часть моего кода запускается под циклом while с условием, что поток все еще жив.
Некоторые заявления:
static int theHour;
static int theMinute;
static int theSecond;
Начало моего время цикла:
while (this.threadAlive)
{
System.out.println("START thread");
theHour = theTime.get(Calendar.HOUR_OF_DAY);
theMinute = theTime.get(Calendar.MINUTE);
theSecond = theTime.get(Calendar.SECOND);
System.out.println("the second is: " + theSecond);
//...
//...
//...
try
{
if (theHour == 12 && theMinute == 39 && (theSecond >= 0 || theSecond < 10) )
{
System.out.println("In the loop");
if (super.connectToDevice())
{
// Send the data command to the device
//out.println(COMMAND_GP);
System.out.println("Simulation of midnight is successful");
// Read and store the data returned from the device
String data = in.readLine();
data = "test gps data";
// Send the data off to be processed
sendDataForProcessing(data);
// Disconnect from the device
super.disconnectFromDevice();
}
}
//Catch any exceptions here
}
В результате в консоли примерно через 10 секунд выполнения:
START thread
the second is: 46
START thread
the second is: 46
START thread
the second is: 46
START thread
the second is: 46
START thread
the second is: 46
START thread
the second is: 46
START thread
the second is: 46
START thread
the second is: 46
START thread
the second is: 46
В результате я get для theSecond
является правильным, но он никогда не обновляется после прохождения цикла снова. Мои объявления определены в глобальном масштабе в классе, и я попытался объявить их как int
, но это не повлияло. Что я делаю неправильно?
Он работает и распечатывает его намного быстрее, чем обновляется системный такт – ControlAltDel
Это было бы правдой, если цикл while был так же прост, как и операторы печати. Список упоминаемого кода относится к подключению к микроконтроллеру, захвату данных, отключению, спящему, а затем возобновлению снова – KS7X
Звучит довольно сложно – ControlAltDel