import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

 

public class TimerTest extends TimerTask {

    int count = 0;


    @Override

    public void run () {

System.out.printf ("%d %s\n", ++count, new Date ()); // every 5sec, call this function

    }

    

    public static void main (String args[]){

        TimerTest timerTask = new TimerTest ();

        Timer timer = new Timer (true);


// every 5sec call run func after 3sec 

 // Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.

      timer.scheduleAtFixedRate (timerTask, 3*1000, 5*1000);

System.out.println ("TimerTask started");


try {

            Thread.sleep (50*1000); // 50sec

        } catch (InterruptedException e) {

            e.printStackTrace ();

        }

timer.cancel ();   

System.out.println ("TimerTask terminated");

    }

}


Reference API : https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html


TimerTest.class


TimerTest.java

'Programming > Android' 카테고리의 다른 글

Activity lifecycle  (0) 2016.02.04
로그인 폼  (0) 2016.02.03
Toast(알림)  (0) 2016.02.03
액티비티 스위칭 프로그램  (0) 2016.02.02
Andriod Studio 설치  (0) 2016.02.02
Posted by 루나s
,