-1

I have a thread that runs every day at a given hour. it gives me this error message :

2014-05-21 03:57:06 [CRITICAL][AlertMgr] : maintenance :

java.lang.IllegalArgumentException: timeout value is negative at java.lang.Thread.sleep(Native Method) at com.orca.pf.tc50.managerutilities.maintenance.MaintenanceManager.backgroundProcess(Unknown Source) at com.orca.pf.tc50.managerutilities.maintenance.MaintenanceManager.access$000(Unknown Source) at com.orca.pf.tc50.managerutilities.maintenance.MaintenanceManager$1.run(Unknown Source) at java.lang.Thread.run(Thread.java:745).

do you know what could cause this.

Thanks

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());
}
mhamdizo
  • 127
  • 1
  • 2
  • 9
  • 2
    Current time is now and the Calendar is in past and you get a negative value if you subtract it. – Praba May 22 '14 at 16:28
  • if calendar is in the past w add one day if (cal.get(Calendar.HOUR_OF_DAY) > HOUR_START) cal.add(Calendar.DAY_OF_YEAR, 1); – mhamdizo May 22 '14 at 16:31

1 Answers1

1

I have a recommendation for you , rather than using Thread.sleep() try looking into Java Timer class . It would simplify your task of scheduling job every x hours. It will save you the effort to compute the amount of time to sleep to schedule the next task. This class lets you focus on the Business logic and takes care of the job of scheduling your task.

Kakarot
  • 4,252
  • 2
  • 16
  • 18