What is the best way to create a background thread that will run every 15 minutes to get data from the database?
Below is the code that will work fine, I think itβs in production, but is there any other way that I have or something that I should know about?
private static void checkDatabaseEveryXMinutes() { new Thread() { public void run() { while (true) { try { Thread.sleep(checkingAfterEveryXMinutes); getDataFromDatabase(); } catch (InterruptedException ex) {
Is there a flaw in using the above code. And how does a ScheduledExecutorService compare with a TimerTask?
Which way is better?
Any example of my code base would be appreciated on this subject if there is any better approach.
source share