Best way to create background thread in java

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) { //log here } catch (Exception e) { //log here } } } }.start(); } 

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.

+6
source share
3 answers

ScheduledExecutorService will return to you a future that has an additional Runnable completion check method. Both have methods to override Runnable. For a repetitive task like this, checking if this is done will probably not make much sense. However, it was introduced using the jdk 1.5 concurrency api, which should definitely be used instead of the older concurrency / thread api (Timer and TimerTask were jdk 1.3). They will be more reliable and efficient. They have a very similar example, as your use case in java doc is here .

here is a sample:

 import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; public class ScheduledTaskExample { private final ScheduledExecutorService scheduler = Executors .newScheduledThreadPool(1); public void startScheduleTask() { /** * not using the taskHandle returned here, but it can be used to cancel * the task, or check if it done (for recurring tasks, that not * going to be very useful) */ final ScheduledFuture<?> taskHandle = scheduler.scheduleAtFixedRate( new Runnable() { public void run() { try { getDataFromDatabase(); }catch(Exception ex) { ex.printStackTrace(); //or loggger would be better } } }, 0, 15, TimeUnit.MINUTES); } private void getDataFromDatabase() { System.out.println("getting data..."); } public void shutdowh() { System.out.println("shutdown..."); if(scheduler != null) { scheduler.shutdown(); } } public static void main(String[] args) { final ScheduledTaskExample ste = new ScheduledTaskExample(); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { ste.shutdowh(); } }); ste.startScheduleTask(); } } 
+11
source

You can try using java.util.TimerTask and java.util.Timer

Example here : -

 Timer t = new Timer(); t.scheduleAtFixedRate( new TimerTask() { public void run() { System.out.println("3 seconds passed"); } }, 0, // run first occurrence immediately 3000); 
+2
source

tieTYT is right. If you use it on an application server, you can use the ejbtimer service for a specific server.

0
source

Source: https://habr.com/ru/post/952239/


All Articles