简体   繁体   中英

Making a task every x time

I use the attached code to send to my db every 5 min some information, some of the information is timestamp. When I look in my db I see records with 6 minute diffrence and even 7, how come? Some told me that my task takes too long.

Anyway my question is how I can force my code to send the info the to the db every 5 min.

*Important thing I have to say that I have a condition on the task which means that sometimes it won't do a thing so the diffrence between the records should be multiples of five.

this.timer.schedule(new Send(), new Date(), TEN_SECONDS*6*5);

class Send extends TimerTask
{
    public void run()
    {
        if(location!=null)
        {
            if (mGeocoderAvailable) 
                address = reverseGeocode(LocationService.this.location);
            if(address != "" && !address.equals(lastAddress))
            {
                lastAddress = address;
                new SendLocation(LocationService.this.id,address);
            }
        }
    }
}

The SendLocation body is like this:

public SendLocation(int id,String address)
{
    // taking care the parameters
    this.start();
}
public void run() 
{ 
    //connect to db
    //send location to db
    this.destroy();
}

时间是在SendLocation线程内安排的,而不是在Send任务中安排的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM