安排任务每天运行 3 次,每次 X 分钟,如果
下面是我尝试的代码示例,我需要每天在 3 个不同的时间运行 1 个任务 X 分钟,假设我有 12、17、20,它应该运行 120 分钟,而且已经是 12:30 :00 因此,如果我打开应用程序,它应该启动任务并且应该运行 90 分钟。
我在这里做错了什么以及我必须更改什么才能执行上述操作?
// here I receive my config with the hours I need to run my task
String[] time = Config.SCHEDULE.split(",");
int runTimeLeft = Config.TIMELEFT;
for (String hour : time)
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
long start = calendar.getTimeInMillis() - System.currentTimeMillis();
ThreadPoolManager.getInstance().scheduleAtFixedRate(new Runnable()
{
public void run()
{
startMyTask();
}
}, start, 24 * 60 * 60 * 1000);
}
Below is a sample of the code I was trying, I need to run 1 task in 3 different times for X minutes every day and let's say I have 12,17,20 and it should run for 120 minutes and it is already 12:30:00 so if i open the application it should start the task and it should run for 90 minutes.
What am I doing wrong here and what do I have to change in order to do the above ?
// here I receive my config with the hours I need to run my task
String[] time = Config.SCHEDULE.split(",");
int runTimeLeft = Config.TIMELEFT;
for (String hour : time)
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
long start = calendar.getTimeInMillis() - System.currentTimeMillis();
ThreadPoolManager.getInstance().scheduleAtFixedRate(new Runnable()
{
public void run()
{
startMyTask();
}
}, start, 24 * 60 * 60 * 1000);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不完全是一个Java问题......
您需要检查当前时间(日历中)减去当前收到的时间是否大于 0 且小于 2 小时,如果是,则运行任务“2 小时减去此差异”,否则您写的内容。
It's not exactly a Java problem...
You need to check if current time (in the calendar) minus current recieved time is more than 0 and less than 2 hours, and if so run the task for "2 hours minus this DIFFERENCE", else what you wrote.