Run java thread at specific time

I have a web application that syncs with a central database four times per hour. The process usually takes 2 minutes. I would like to start this process as a thread in X: 55, X: 10, X: 25 and X: 40, so that users know that in X: 00, X: 15, X: 30 and X: 45 they have a clean copy of the database. It is only about managing expectations. I went through the executor in java.util.concurrent , but the scheduling is done using scheduleAtFixedRate , which, it seems to me, does not guarantee when it really does work in terms of hours. I could use the first delay to run Runnable , so that the first is close to the start time and schedule for every 15 minutes, but it seems like it probably diverges in time. Is there an easier way to schedule a stream 5 minutes before every quarter hour?

+7
java multithreading concurrency
source share
3 answers

You can enable the runnable "next run" schedule.

For example,

 class Task implements Runnable { private final ScheduledExecutorService service; public Task(ScheduledExecutorService service){ this.service = service; } public void run(){ try{ //do stuff }finally{ //Prevent this task from stalling due to RuntimeExceptions. long untilNextInvocation = //calculate how many ms to next launch service.schedule(new Task(service),untilNextInvocation,TimeUnit.MILLISECONDS); } } } 
+9
source share

Quartz will be of great help since you are using an Internet-based application. This will ensure accurate time planning that you need.

Quartz is a full-featured, open-source job scheduling service that can be integrated or used along side almost any Java EE or Java SE application - from the smallest standalone application to the largest e-commerce. Quartz can be used to create simple or complex graphics to dozens, hundreds or even tens of thousands of jobs; Jobs tasks are defined as standard Java components that can run almost anything you can program for them. The quartz scheduler includes many enterprise-class features, such as JTA transactions and clustering.

+2
source share

TimerTask handles this case.

See schedule (TimerTask, Date)

If you do not want to continue scheduling tasks, you may need to study a task scheduling tool such as Quartz .

+1
source share

All Articles