タスクをフルポイントで実行
1401 ワード
OneHour.java :
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
public class OneHour {
private final static long JOB_INTERNAL = 1000 * 60 * 60;// , ,
public static void main(String[] args) {
Timer timer = new Timer();
Calendar currentTime = Calendar.getInstance();
currentTime.setTime(new Date());
int currentHour = currentTime.get(Calendar.HOUR);
currentTime.set(Calendar.HOUR, currentHour + 1);
currentTime.set(Calendar.MINUTE, 0);
currentTime.set(Calendar.SECOND, 0);
currentTime.set(Calendar.MILLISECOND, 0);
//
/*
currentTime.set(Calendar.HOUR, currentTime.get(Calendar.HOUR));
currentTime.set(Calendar.MINUTE, currentTime.get(Calendar.MINUTE));
currentTime.set(Calendar.SECOND, currentTime.get(Calendar.SECOND));
currentTime.set(Calendar.MILLISECOND, currentTime.get(Calendar.MILLISECOND));
*/
Date NextHour = currentTime.getTime();
System.out.println(NextHour);
timer.scheduleAtFixedRate(new MyTask(), NextHour, JOB_INTERNAL);
}
}
MyTask.JAvaクラス:
import java.util.Date;
import java.util.TimerTask;
class MyTask extends TimerTask {
@Override
public void run() {
System.out.println("mytask: " + new Date());
try {
Thread.sleep(10000); // ,
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}