我在下面的代码中遇到了问题。
//Send Creation email
ListServDAO.sendCreateEmail(orgId, full, request.getSession().getServletContext());
//Force a 1 minute pause
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
//Send add members email
ListServDAO.sendAddMembersEmail(orgId, request.getSession().getServletContext());
}
}, 0, 60 * 1000);在sendCreateEmail函数调用后,sendAddMembersEmail函数不会等待1分钟即可发送。我不确定为什么它在读完java API之后也不等待。我不希望使用Thread.sleep方法,因为我希望用户能够在等待电子邮件发送时使用应用程序。
发布于 2012-05-23 01:58:55
你有一个额外的零。
您调用的是Delayed with a Fixed-Delay Repeat Execution,而您想要的是一个Single Delay Non Repeat execution。
发布于 2012-05-23 01:58:09
您是否打算重复发送电子邮件?如果不是,为什么要使用耗时2长的方法(即“每隔<period>毫秒重复运行一次此任务”)?使用schedule(task, delay)方法(并使用非零延迟)。
发布于 2012-05-23 01:59:28
第二个参数是初始延迟,你将其作为60000传递,因此它会立即执行它,传递1秒,让它等待一分钟,然后发送。
由于您只希望它只发送一次使用,因此调用Timer.schedule(TimerTask task, long delay)
离题:
您应该考虑使用ScheduledExecutorService而不是Timer。参见Oracle Tutorial。
https://stackoverflow.com/questions/10707361
复制相似问题