每x次制作一個任務

[英]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.

我使用附加的代碼每隔5分鍾向我的數據庫發送一些信息,其中一些信息是時間戳。當我查看我的數據庫時,我看到記錄有6分鍾差異,甚至7分鍾,怎么樣?有些人告訴我,我的任務需要太長時間。

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

無論如何,我的問題是如何強制我的代碼每隔5分鍾將信息發送到數據庫。

*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:

SendLocation主體是這樣的:

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

1 个解决方案

#1


0  

The time was scheduled inside the SendLocation thread instead from the Send task.

時間安排在SendLocation線程內,而不是Send任務。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/01/25/72f04d19ba3c2378be31d57ea65e80f7.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com