我希望每天(每24小时)运行一项特定任务.我使用了一个预定的执行程序服务,但是在我用20秒测试它以查看任务是否会运行之后,它没有.难道我做错了什么?
任何帮助将不胜感激.
ScheduledExecutorService scheduler = Executors
.newScheduledThreadPool(1);
scheduler.scheduleWithFixedDelay(new TimerTask() {
public void run() {
ArrayList<Integer> people = new ArrayList<Integer>();
try {
shelf.g.setOverdue();
for (int i = 1; i < 1000; i++) {
if (shelf.book[i] != null
&& shelf.book[i].checkedOut != null) {
if (shelf.book[i].overdue == true) {
for (int i2 = 0; i < 600; i++) {
if (account.person[i] == null) {
account.person[i] = new account();
}
if (account.person[i2].name
.equalsIgnoreCase(shelf.book[i].personName)) {
people.add(i2);
}
}
}
}
}
Set<Integer> s = new LinkedHashSet<Integer>(people);
people = new ArrayList<Integer>(s);
ArrayList<String> books = new ArrayList<String>();
Properties props = new Properties();
Session session = Session.getInstance(props);
MimeMessage msg = new MimeMessage(session);
Transport t = null;
Address from = new InternetAddress(
"LGCCLibrary42@gmail.com", "LGCC Library");
for (int i = 0; i < people.size(); i++) {
for (int i2 = 1; i2 < 1000; i2++) {
if (shelf.book[i2] != null
&& shelf.book[i2].checkedOut != null) {
if (shelf.book[i2].overdue == true) {
if (account.person[people.get(i)].name
.equalsIgnoreCase(shelf.book[i2].personName)) {
books.add("Book " + i2 + " - " + shelf.book[i2].bookName
+ "\n");
}
}
}
}
String thePerson = account.person[people.get(i)].name;
Address to = new InternetAddress(account.person[people
.get(i)].eMail);
msg.setText(thePerson
+ " , you have the following books overdue"
+ "\n" + books.toString().replace("[", "").replace("]", ""));
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO, to);
msg.setSubject("LGCC library overdue books");
t = session.getTransport("smtps");
t.connect("smtp.gmail.com", "LGCCLibrary42", "4JesusChrist");
t.sendMessage(msg, msg.getAllRecipients());
books.clear();
}
t.close();
} catch (UnsupportedEncodingException ex) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "Something went wrong",
"Alert", JOptionPane.ERROR_MESSAGE);
} catch (AddressException ex) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "Something went wrong",
"Alert", JOptionPane.ERROR_MESSAGE);
} catch (MessagingException ex) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "Something went wrong",
"Alert", JOptionPane.ERROR_MESSAGE);
}
}
}, 0, 24, TimeUnit.HOURS);
解决方法:
正如@JeremeyFarrell所说,使用Runnable而不是TimerTask;使用TimerTask没有任何功能或好处.
我已经简化了你的代码,它只是没有问题的工作:
ScheduledExecutorService scheduler = Executors
.newScheduledThreadPool(1);
scheduler.scheduleWithFixedDelay(new Runnable() {
public void run() {
System.out.println("Do something useful");
}
}, 0, 1, TimeUnit.SECONDS);
您可能在scheduleWithFixedDelay的Javadoc中找到问题的最可能原因:
If any execution of the task encounters an exception, subsequent
executions are suppressed.
您可能有一个异常,可能是RuntimeException(例如NullPointerException),它停止了进一步的调用.
解决这个问题的一种方法是捕获所有异常并记录它们.
scheduler.scheduleWithFixedDelay(new Runnable() {
public void run() {
try {
doTheRealWork(); // Such as "sendEmail()"
} catch (Exception e) {
e.printStackTrace(); // Or better, use next line if you have configured a logger:
logger.error("Exception in scheduled e-mail task", e);
}
}
}, 0, 1, TimeUnit.SECONDS);
(注意:当然,一旦你对事情按预期工作,请将1,TimeUnit.SECONDS替换为24,TimeUnit.HOURS)
有几点需要指出:
>您从不是UI线程的Thread调用JOptionPane.showMessageDialog. Swing不支持从主UI线程以外的任何线程执行UI工作;如果你仍然这样做,你可以获得各种竞争条件.
>在任何情况下,即使这不是问题,您也会阻止用户交互的预定线程;在您继续申请之前,有人必须按“确定”
>但这不会导致您的问题.
标签:java,scheduled-tasks 来源: https://codeday.me/bug/20190725/1528887.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。