Below are the different ways to schedule a task in java
1. Timer-
Sample code : https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/scheduler/PrintTimeTimerTask.java
a. Task needs to extend TimerTask. So you can extend any other classes
b. It has only one execution thread. Long running tasks may delay other tasks
c. Runtime execption might kill that single thread which may cause the timer dead
2. Cron4j scheduler
Sample code: https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/scheduler/PrintTimeSchedulerTask.java
a. Task just needs to be Runnable.
b. It supports the very famous cron expression, which makes easy to understand/schedule
c. you can start and stop a schedule at any time. You can control any ongoing task.
d. Can implement our own scheduler listeners by implementing SchedulerListener interface
3. ExecuterService
Sample code: https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/scheduler/PrintTimeExecutorTask.java
a. Task just needs to be Runnable
b. It can catches runtime exceptions
How to initilize the scheduler for the tasks??
https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/scheduler/ScheduleThread.java
1. Timer-
Sample code : https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/scheduler/PrintTimeTimerTask.java
a. Task needs to extend TimerTask. So you can extend any other classes
b. It has only one execution thread. Long running tasks may delay other tasks
c. Runtime execption might kill that single thread which may cause the timer dead
2. Cron4j scheduler
Sample code: https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/scheduler/PrintTimeSchedulerTask.java
a. Task just needs to be Runnable.
b. It supports the very famous cron expression, which makes easy to understand/schedule
c. you can start and stop a schedule at any time. You can control any ongoing task.
d. Can implement our own scheduler listeners by implementing SchedulerListener interface
3. ExecuterService
Sample code: https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/scheduler/PrintTimeExecutorTask.java
a. Task just needs to be Runnable
b. It can catches runtime exceptions
How to initilize the scheduler for the tasks??
https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/scheduler/ScheduleThread.java