日程管理
最后,经过一些尝试和错误,我成功地使其按照我的意愿工作。
但现在我想听听你的建议,让代码更具可读性和简单性,似乎做了很多不必要的代码来归档我想要的东西。
它的基本作用是,如果您在计划任务应该运行的时间打开服务器应用程序,它将启动该任务并让它运行到它应该启动时剩下的时间,否则它将被计划运行在它应该运行的时间。
因此,如果计划时间为 13:00:00,应运行 120 分钟,而您在 13:30 启动应用程序,它将运行 90 分钟。如果您在该时间之后启动,通常会安排在第二天的 13:00:00。
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
long start_time = calendar.getTimeInMillis() - System.currentTimeMillis();
if (start_time < 0)
{
long minutes = (start_time*-1) / (60 * 1000);
if (minutes > 0 && minutes < 120)
{
runTimeLeft = 120 - minutes;
ThreadPoolManager.getInstance().schedule(new Runnable()
{
public void run()
{
myTask();
}
}, 0);
}
else
runTimeLeft = 0;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour+24);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
start_time = calendar.getTimeInMillis() - System.currentTimeMillis();
}
ThreadPoolManager.getInstance().scheduleAtFixedRate(new Runnable()
{
public void run()
{
myTask();
}
}, start_time, 24 * 60 * 60 * 1000);
所以我现在的问题是我可以对上面的代码进行哪些改进?
Finally after some trial and errors I have managed to make it work as I wanted.
But now I would like your advice to make the code more readable and simple it seems a made a lot of unnecessary code to archive what I wanted.
What this basicly do is, if you turn on the server app at a time a schedule task should be running, it will start the task and let it run for the time left from when it should have started otherwise it will be schedule to run at the hour it is supposed to run.
So if the schedule time is 13:00:00 and should run for 120 minutes and you start the app at 13:30 it will run for 90 minutes. If you start it after that time, it will be normally schedule for the next day 13:00:00.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
long start_time = calendar.getTimeInMillis() - System.currentTimeMillis();
if (start_time < 0)
{
long minutes = (start_time*-1) / (60 * 1000);
if (minutes > 0 && minutes < 120)
{
runTimeLeft = 120 - minutes;
ThreadPoolManager.getInstance().schedule(new Runnable()
{
public void run()
{
myTask();
}
}, 0);
}
else
runTimeLeft = 0;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour+24);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
start_time = calendar.getTimeInMillis() - System.currentTimeMillis();
}
ThreadPoolManager.getInstance().scheduleAtFixedRate(new Runnable()
{
public void run()
{
myTask();
}
}, start_time, 24 * 60 * 60 * 1000);
So my question here now is what could I improve on the above code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要单独使用
java.util.Timer
,而是尝试将其与TimerTask
一起使用。 IBM 有一篇关于此的好文章。查看此链接: http://www.ibm.com/ developerworks/java/library/j-schedule.html
该代码也被共享,并且似乎适用于琐碎的日常工作。
Instead of using
java.util.Timer
alone, try using it withTimerTask
. There is a good article from IBM on this.Have a look at this link: http://www.ibm.com/developerworks/java/library/j-schedule.html
The code is also shared and seems to work for trivial routine job.
将其用于第一个方法:
这将创建一个简单的计时器,该计时器将在 24 小时内调用
myTask()
,然后每 24 小时调用一次。不过,您的其他要求有点不同。如果我正确理解您的描述,您基本上希望您的应用程序始终在凌晨 12:00 执行某些任务(如果它恰好启动并运行)。如果您不关心精确到毫秒的精度,则可以非常简单地通过启动一个一分钟周期的计时器并检查每个时钟周期中的当前系统时间(当您到达凌晨 12:00 时)来实现此目的。运行你的日常任务。
一种更奇特的方法是与操作系统进行交互,以便它在预先安排的时间回调您的应用程序(如果需要,甚至可能启动您的应用程序),但这种事情是特定于操作系统/平台的(您没有指定) 。
更新:我对Linux一无所知,但看起来
cron
工作正是您正在寻找的。请参阅此问题:运行用以下语言编写的计划任务Linux 服务器上的 java
Use this instead for your first method:
This will create a simple timer that will call
myTask()
in 24 hours, and then every 24 hours after.Your other requirement is a little different, though. If I understand your description correctly, you basically want your app to always execute some task at 12:00 AM if it happens to be up and running. If you don't care about down-to-the-millisecond accuracy for this, you could achieve it very simply by starting a Timer with a one minute period and checking the current system time in each tick - when you hit 12:00 AM run your daily task.
A fancier way would involve interacting with the OS such that it makes callbacks to your application at pre-scheduled times (possibly even starting your app if necessary), but this kind of thing is OS/platform specific (which you didn't specify).
Update: I know nothing about Linux, but it looks like a
cron
job is what you're looking for. See this question:Running a scheduled task written in java on a linux server