0
while(true){
    try 
    {
        if(Calendar.DATE == X){
            startTask();
        } 
        long delay = timeUntilNextCheck();
        Thread.sleep(delay);
    } 
    catch (Throwable t) 
    {

    }
}

I have a program that requires a specific task to run on a specific day of the month. After the task is run (Or if its not that day) the thread sleeps until tomorrow where it will check again.

However, I am getting a Dead code warning because part of the code is only ran on a specific day of the month.

I had a read up on what this warning is and I found that in some cases the compiler doesn't compile dead code. So my question is, will this always be compiled?

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674

4 Answers4

7

Can you tell us what X is?

if(Calendar.DATE == X)

If X is some constant representing day of month, this will not work because you are comparing Calendar.DATE constant with another constant X. Let me guess, your code is something like:

if(Calendar.DATE == 17)  //run on 17th of every month

which translates into:

if(5 == 17)  //I see dead code

Compiler gives you a hint that this condition will never be satisfied (and might not bother compiling the if statement body).

Instead you should test:

if(new GregorianCalendar().get(Calendar.DATE) == 17)

Or even better use Quartz. You would be surprised how many mistakes can you make with such a simple code (think: server timezone, daylight saving time...)

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 2
    why `new GregorianCalendar()` rather then `Calendar.getInstance()`? – Sean Patrick Floyd Sep 21 '11 at 10:05
  • Tricky question, indeed. I think in some strange universe `Calendar.getInstance()` might return something that isn't `GregorianCalendar`. This is not a problem *per se*, however if this other calendar system does not have a notion of *day of the month* (or even *month* altogether), you would get nasty runtime exception. One of the several flaws of Java date/calendar abstractions. Am I not mistaken? – Tomasz Nurkiewicz Sep 21 '11 at 10:09
5

However, I am getting a Dead code warning because part of the code is only ran on a specific day of the month.

No, you're getting a dead code warning becase startTask(); will never run. Calendar.DATE is an internal index constant of the Calendar class with the value 5. To get the current day of the month, use this code: Calendar.getInstance().get(Calendar.DAY_OF_MONTH)

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
1

I presume the dead code is the line startTask();

If the compiler can detect that this is unreachable, it is probably because X (whatever it is) can never take the same value as Calendar.DATE, which is always 5. This is "field number for get and set indicating the day of the month." according to the Javadoc, not the current day of the month, which you could get for example using

Calendar.getInstance().get(Calendar.DATE)

You might want to look at something like java.util.Timer by the way.

Ben
  • 2,348
  • 2
  • 19
  • 28
  • In my timeUntilNextCheck() method I am creating two calendars (todays and tomorrows) and then calculating the difference between them. Is this good practice or is java timer the way to go? –  Sep 21 '11 at 10:08
  • With java.util.Timer (or something more sophisticated like Quartz) you can just create a task that will run at a given time in the future, without subtracting dates. The Calendar and related classes in the Java standard library are notoriously awkward to use, so Joda-Time is often preferred. – Ben Sep 21 '11 at 10:17
0

Go for quartz and configure CronTrigger that would be far better

jmj
  • 237,923
  • 42
  • 401
  • 438