it916@planning-ho-pc:~$ pico RepeatTask.java
import java.util.Timer; import java.util.TimerTask; public class RepeatTask { public static void main(String[] argv) throws Exception { int delay = 5000; // delay for 5 sec. int period = 1000; // repeat every sec. Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { System.out.println("doing"); } }, delay, period); } }
Kompilasi program kemudian jalankan. Anda akan melihat output yang dieksekusi secara terus menerus.
it916@planning-ho-pc:~$ javac RepeatTask.java it916@planning-ho-pc:~$ java RepeatTask doing doing doing doing doing doing doing doing ... ... ...
Demikian dan selamat mencoba.
Sumber : http://www.java2s.com/Code/Java/Development-Class/SchedulingaTimerTasktoRunRepeatedly.htm
Tidak ada komentar:
Posting Komentar