0

I'm testing something and I want print out execution time with this code:

    private Runnable runnerLog = new Runnable() {       
        @Override
        public void run() {           

            String timeStamp = new SimpleDateFormat("HH:mm:ss yyyy-dd-MM").format(Calendar.getInstance().getTime());
            System.out.println("Current time: " + timeStamp);

            mHandlerLogger.postDelayed(runnerLog, mInterval);
        }};

The result should be like:

13:45:10 13:45:12 13:45:14 13:45:16 and so on...

but i noticed that sometimes is not difference 2 seconds, but three:

13:45:10 13:45:12 13:45:15 13:45:17 13:45:19 13:45:21 13:45:24

What is the reason for this situation? Is there better solution for executing something each X seconds?

Josef
  • 2,648
  • 5
  • 37
  • 73

2 Answers2

0

try this

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    Thread myThread = null;

    Runnable runnable = new CountDownRunner();
    myThread= new Thread(runnable);   
    myThread.start();

}

public void doWork() {
    runOnUiThread(new Runnable() {
        public void run() {
            try{
                TextView txtCurrentTime= (TextView)findViewById(R.id.lbltime);
                    Date dt = new Date();
                    int hours = dt.getHours();
                    int minutes = dt.getMinutes();
                    int seconds = dt.getSeconds();
                    String curTime = hours + ":" + minutes + ":" + seconds;
                    txtCurrentTime.setText(curTime);
            }catch (Exception e) {}
        }
    });
}


class CountDownRunner implements Runnable{
    // @Override
    public void run() {
            while(!Thread.currentThread().isInterrupted()){
                try {
                doWork();
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                }catch(Exception e){
                }
            }
    }
}
user3355820
  • 272
  • 1
  • 8
  • I have tried with Countdown class and with this sample and the result is same. The interval between triggering was 3 seconds. In on of the outputs the difference was 2 seconds and in some 4 seconds. The rest are mostly with the same difference with 3 seconds. I'm working on application that make some logs each X seconds and store values in file with time so it's important to have correct data in matter of time. – Josef Apr 10 '14 at 12:18
  • use Calendar c = Calendar.getInstance(); int seconds = c.get(Calendar.SECOND); – user3355820 Apr 10 '14 at 12:24
  • Could you be more specific what exact to do with Calendar class? – Josef Apr 10 '14 at 12:31
  • see [this](http://stackoverflow.com/questions/2271131/display-the-current-time-and-date-in-an-android-application) – user3355820 Apr 10 '14 at 12:33
  • Maybe it's problem because I have two runnables and both of the works different things. Maybe somehow first runnable affects second... – Josef Apr 11 '14 at 06:45
0

please try this:

private Handler myhandler ;
private long RETRY_TIME = xx; 
private long START_TIME = yy; 


//oncreate
myhandler= new Handler();
myhandler.postDelayed(myRunnable, START_TIME);


//your runnable class

private Runnable myRunnable = new Runnable() {
  public void run() { 
    //doing something      
 myhandler.postDelayed(myRunnable, RETRY_TIME);
 }
 };
CompEng
  • 7,161
  • 16
  • 68
  • 122
  • No... Same problem. In the beginning runs fine but after some time the difference is 4 seconds and than continues normally and again appears difference for about 4 seconds... I don't really know what makes interruption to show wrong times... – Josef Apr 11 '14 at 06:41
  • you doing someting after x seconds ? so it makes the difference , I mean the cpu when doing that job lose time? – CompEng Apr 11 '14 at 06:59
  • As I previously wrote there are two runnables. On runnable is activating method that communicates with some device connected over OTG, reads and store date. Second runnable runs every X seconds and take values from variables where are stored measured values. That second runnable is critical because it doesn't triggering in exact intervals. I'm totally out of mind why is this happening. – Josef Apr 11 '14 at 07:19