noo*_*mer 4 java scheduled-tasks
我希望每天(每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);
Run Code Online (Sandbox Code Playgroud)
Erw*_*idt 11
正如@JeremeyFarrell所说,用一个Runnable而不是一个TimerTask; 使用a没有任何功能或好处TimerTask.
我已经简化了你的代码,它只是没有问题的工作:
ScheduledExecutorService scheduler = Executors
.newScheduledThreadPool(1);
scheduler.scheduleWithFixedDelay(new Runnable() {
public void run() {
System.out.println("Do something useful");
}
}, 0, 1, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
您可能会在以下Javadoc中找到问题的最可能原因scheduleWithFixedDelay:
如果任务的任何执行遇到异常,则后续执行被禁止.
您可能有一个异常,可能是RuntimeException(例如a 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);
Run Code Online (Sandbox Code Playgroud)
(注:当然,替换1, TimeUnit.SECONDS用24, TimeUnit.HOURS,一旦你感到满意的是工作的事情如预期)
有几点需要指出:
JOptionPane.showMessageDialog从一个不是UI线程的线程调用的.Swing不支持从主UI线程以外的任何线程执行UI工作; 如果你仍然这样做,你可以获得各种竞争条件.