0

I try to excute the code every hour

This is for java in applicationRunner that can be run with the server is started.

@Component
public class TestApplicationRunner implements ApplicationRunner {
    // some Autowired


    @Override
    public void run(ApplicationArguments args) throws Exception {

        TimerTask repeatedTask = new TimerTask() {
            @Override
            public void run() {
                System.out.println("Task performed on " + new Date()); //now

                //Some code that updates the database
            }
        };

        Timer timer = new Timer();
        Calendar date = Calendar.getInstance();

        LocalDateTime nextHour = LocalDateTime.now().plusHours(1).truncatedTo(ChronoUnit.HOURS);
        long result = LocalDateTime.now().until(nextHour, ChronoUnit.MILLIS);

        System.out.println(date.getTime());
        long period = 1000L * 60L * 60L;
        timer.scheduleAtFixedRate(repeatedTask, result, period);

    }
}

if I run this code at 12:34, the result is

        Task performed on 13 : 00 
        Task performed on 14 : 00 
        Task performed on 14 : 00 
        Task performed on 15 : 00 
        Task performed on 15 : 00 
        Task performed on 15 : 00 
        Task performed on 16 : 00 
        Task performed on 16 : 00 
        Task performed on 16 : 00 
        Task performed on 16 : 00
               .....

I want to receive

        Task performed on 13 : 00 
        Task performed on 14 : 00 
        Task performed on 15 : 00 
        Task performed on 16 : 00 
        Task performed on 17 : 00 
               .....

I think there are more thread than one, but I can't understand why they have several thread. I add my code. thank you for help

Michael A. Schaffrath
  • 1,992
  • 1
  • 14
  • 23

1 Answers1

1

First of all: java.util.Timer ist outdated. It is recommended to use java.util.concurrent.ScheduledThreadPoolExecutor instead:

Java 5.0 introduced the java.util.concurrent package and one of the concurrency utilities therein is the ScheduledThreadPoolExecutor which is a thread pool for repeatedly executing tasks at a given rate or delay. It is effectively a more versatile replacement for the Timer/TimerTask combination, as it allows multiple service threads, accepts various time units, and doesn't require subclassing TimerTask (just implement Runnable). Configuring ScheduledThreadPoolExecutor with one thread makes it equivalent to Timer.

Apart from that, it looks to me like your problem is not with the scheduling. Your code looks correct. My guess is that your TestApplicationRunner is started multiple times, and therefore multiple Timers are created.

Michael A. Schaffrath
  • 1,992
  • 1
  • 14
  • 23
  • i added my code. i will change to scheduledTread instead of using timer after your answers – his_ mogologue Jul 25 '19 at 07:13
  • I had a look at the code - you were right to leave it out in the first place ;-) It doesn't add any insights to your question. I edited it back. – Michael A. Schaffrath Jul 25 '19 at 08:00
  • Could you also post the part of your logs where `System.out.println(date.getTime());`gets printed? **If** `TestApplicationRunner` is started multiple times, there should be multiple lines printing the time. – Michael A. Schaffrath Jul 25 '19 at 08:04