Java how to run code every 3 seconds// how to run the code at regular intervals in java
/**
* @author lautturi.com
* Java example: java run code at interval time
*/
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Lautturi {
public static void main(String[] args) {
/* Perform this runnable code every 5 seconds in this example */
Runnable runnable = new Runnable() {
public void run() {
System.out.println("Hello Java!!");
}
};
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(runnable, 0, 3, TimeUnit.SECONDS);
}
}
Soecru:www.lautturi.com