开发者

Thread testing for time

开发者 https://www.devze.com 2022-12-28 19:12 出处:网络
I\'m making a thread for my application that开发者_运维技巧\'s going to do an exit operation at a given time (only hours and minutes, day/month doesn\'t matter). Is this the right way to do it, and al

I'm making a thread for my application that开发者_运维技巧's going to do an exit operation at a given time (only hours and minutes, day/month doesn't matter). Is this the right way to do it, and also the right way to test for time? I'm testing for a 24 hour clock by the way, not AM / PM.

I'm then in another class going to call this something like new Thread(new ExitThread()).start();

public class ExitThread implements Runnable {



public long getDate() {
    Date thisdate = new Date(System.currentTimeMillis());
    return thisdate.getTime();
}


public long exitDate() {
    Date exitdate = new Date(System.currentTimeMillis());
    exitdate.setHours(23);
    exitdate.setMinutes(30);
    exitdate.setSeconds(0);

    return exitdate.getTime();
}

public long sleepTime() {
    Calendar cal = Calendar.getInstance();
    long now = cal.getTime().getTime();

    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 30);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    long endMillis = cal.getTime().getTime();
    long timeToSleep = endMillis - now;

    return timeToSleep;
}

@Override
public void run() {

    while (true) {
        try {
            Thread.sleep(sleepTime());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (getDate() >= exitDate()) {
            // System exit method here
        }
    }
}

}


You might want to recalculate currentTime within the while loop :-)

Instead of waiting 10 seconds before checking the end condition, you can determine the millis that correspond to your end time, and wait for the number of millies between now and the end time.

If your endtime would be between 235950 and 235959 you run the risk of missing it.

update

You can determine the number of millis to wait in this way:

Calendar cal = Calendar.getInstance();
long now = cal.getTime().getTime();

cal.set(Calendar.HOUR_OF_DAY,   23);
cal.set(Calendar.MINUTE,        30);
cal.set(Calendar.SECOND,        0);
cal.set(Calendar.MILLISECOND,   0);

long endMillis = cal.getTime().getTime();
long timeToSleep = endMillis - now;

Note that you need to calculate this within the while loop also, because the sleep can be interrupted the next iteration will need a smaller timeToSleep.


For testability, you should inject two things: a "sleeper" and a "clock". These could be within the same interface if you want, or separate. The production implementation would just use Thread.sleep and System.currentTimeMillis, but it means you can create fake implementations too, which make the code testable.


Perhaps you should consider using a Timer

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号