需要在java中以特定的时间间隔运行特定的功能 [英] need to run specific functionality at specific interval in java
问题描述
我有一个特定的要求。
I have a specific requirement.
我有一个通知功能,可以将电子邮件发送给所有系统客户端。
代码用java编写。
I have a notification functionality which sends the email to all the systems clients. The code is written in java.
我想要做的是,我想执行特定的工作,以特定的间隔发送电子邮件,如一次2天或一周或一个月一次。
What i want to do is, i want to execute that specific job which sends the email at specific intervals like once in a 2 days or once in a week or a month.
截至目前,我还没有关于如何做到这一点的线索。我们正在使用Quartz,但我认为现在不符合我的要求。
As of now i don't have a clue about how to do this. We are using Quartz but I think that wont fit into my requirement as of now.
如果有人有任何想法,请告诉我。
Please let me know if anyone has any idea.
推荐答案
为什么不使用 ScheduledThreadPoolExecutor
,允许您安排将来执行的任务。
Why not use a ScheduledThreadPoolExecutor
which allows you to schedule tasks to execute in future.
ScheduledExecutorService scheduledExecutorService =
Executors.newScheduledThreadPool(5);
ScheduledFuture scheduledFuture =
scheduledExecutorService.schedule(new Callable() {
public Object call() throws Exception {
System.out.println("Executed!");
return "Called!";
}
},
5,
TimeUnit.SECONDS);
这篇关于需要在java中以特定的时间间隔运行特定的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!