Studyon Minte9.com
Java SE




Schedule a task




package testexample; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Test { public static void main(String[] args) { // Create a service with 3 threads. ScheduledExecutorService execService = Executors.newScheduledThreadPool(3); // --- Look Here --- // // Schedule a task to run every 5 seconds with no initial delay. execService.scheduleAtFixedRate(new Runnable() { public void run() { DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); System.out.println("----------------------------------------------------------"); System.out.println("Command 1 - " + dateFormat.format(date)); } }, 0L, 5L, TimeUnit.SECONDS); // 5 sec // --- Look Here --- // // Schedule another task execService.scheduleAtFixedRate(new Runnable() { public void run() { DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); System.out.println("----------------------------------------------------------"); System.out.println("Command 2 - " + dateFormat.format(date) + " -- "); } }, 0L, 6L, TimeUnit.SECONDS); // 6 sec // --- Look Here --- // } }