System.out.println("The time is : " + new Date());
executor.schedule(task1, 5 , TimeUnit.SECONDS);
executor.schedule(task2, 10 , TimeUnit.SECONDS);
try {
executor.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
e.printStackTrace();
}
executor.shutdown();
}
}
The time is : Tue Jan 25 16:14:07 IST 2017 Doing a task during : Demo Task 1 - Time - Tue Jan 25 16:14:12 IST 2017 Doing a task during : Demo Task 2 - Time - Tue Jan 25 16:14:17 IST 2017
System.out.println("The time is : " + new Date());
ScheduledFuture<?> result = executor.scheduleAtFixedRate(task1, 2, 5, TimeUnit.SECONDS);
try {
TimeUnit.MILLISECONDS.sleep(20000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
executor.shutdown();
}
}
Output:
The time is : Tue Jan 25 16:20:12 IST 2017 Doing a task during : Demo Task 1 - Time - Tue Jan 25 16:20:14 IST 2017 Doing a task during : Demo Task 1 - Time - Tue Jan 25 16:20:19 IST 2017 Doing a task during : Demo Task 1 - Time - Tue Jan 25 16:20:24 IST 2017 Doing a task during : Demo Task 1 - Time - Tue Jan 25 16:20:29 IST 2017