我有一个线程,每天在给定的时间运行。它给了我这个错误信息:
2014-05-21 03:57:06 [CRITICAL][AlertMgr]: maintenance:
. lang。IllegalArgumentException:超时值为负值在java.lang.Thread。睡眠(本地方法)在com.orca.pf.tc50.managerutilities.maintenance.MaintenanceManager.backgroundProcess(未知源)在com.orca.pf.tc50.managerutilities.maintenance.MaintenanceManager.access 000美元(未知源)在com.orca.pf.tc50.managerutilities.maintenance.MaintenanceManager 1.美元(未知源)java.lang.Thread.run (Thread.java: 745) .
你知道是什么原因引起的吗?
感谢private void backgroundProcess()
{
Calendar cal;
cal = Calendar.getInstance();
log.addHelpField("AlertMgr");
/*
* Synchronise le calendrier a 3:00AM
*/
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
if (cal.get(Calendar.HOUR_OF_DAY) > HOUR_START)
{
/*
* wait till tomorrow
*/
cal.set(Calendar.HOUR_OF_DAY, HOUR_START);
cal.add(Calendar.DAY_OF_YEAR, 1);
}
else
{
cal.set(Calendar.HOUR_OF_DAY, HOUR_START);
}
do
{
try
{
/*
* Calculate the time to wait
*/
long timeToWait = cal.getTimeInMillis() - System.currentTimeMillis();
Thread.sleep(timeToWait);
processAllEntries();
/*
* wait for tomorrow
*/
cal.add(Calendar.DAY_OF_YEAR, 1);
/*
* Clear
*/
Thread.interrupted();
}
catch(InterruptedException i)
{
Thread.currentThread().interrupt();
}
catch(Exception ex)
{
log.addException(LogLevel.CRITICAL, "maintenance", ex);
}
}
while(!th.isInterrupted());
}
我有一个建议,而不是使用Thread.sleep()尝试查看Java Timer类。它会简化你每x小时安排工作的任务。这将节省你计算睡眠时间来安排下一个任务的努力。这个类让您专注于业务逻辑,并负责安排任务的工作。