I need to sleep a long time (days) in Java

I have an application that needs to do one thing and one only on a schedule, and it can easily calculate the time it needs to complete. You may not need to complete this task for several days or months, but it will probably work in every other way every few milliseconds.

I am looking for a simple easy approach to planning a single task.

+3
source share
6 answers

If your java application is designed to run continuously, you can use Timer to schedule execution

, . cron/task scheduler - .

+4

Unix cron at.

Windows .

Java- Timer API.

Java- quartz.

+11

, cron, , . , . - ( ), , . , ballpark:

import java.util.*;

public abstract class TimeMonitor extends Thread
    {
        protected double execution_time;
        protected boolean execute_at_startup;

        public TimeMonitor()
            {
                execute_at_startup = false;
            }

        public TimeMonitor(double time)
            {
                execution_time = time;
                execute_at_startup = false;
            }

        public void startTimer()
            {
                setPriority(Thread.MIN_PRIORITY);
                start();
            }

        public void setTime(double time)
            {
                execution_time = time;
            }

        public void executeAtStartup()
            {
                execute_at_startup = true;
            }

        public void run()
            {
                if (execute_at_startup)
                    doTimedAction();

                while (true)
                    {
                        long runtime = (long)(execution_time * 3600 * 1000);
                        long now     = getTime();
                        long sleep   = 0;

                        // Calculate how long to way until first run
                        if (runtime > now)
                            sleep = runtime - now;
                        else
                            sleep = 24 * 3600 * 1000 - now + runtime;

                        try
                            {
                                Thread.sleep(sleep);
                            }
                        catch (InterruptedException e)
                            {
                                logError("Wait thread has been interrupted.", e);
                                continue;
                            }

                        doTimedAction();
                    }
            }

        /**
         * Calculates number of milliseconds from start of the day.
         *
         * @return Number of milliseconds.
         */
        private long getTime()
            {
                Calendar cal = Calendar.getInstance();
                int hours   = cal.get(Calendar.HOUR_OF_DAY);
                int minutes = cal.get(Calendar.MINUTE);
                int seconds = cal.get(Calendar.SECOND);
                int millis  = cal.get(Calendar.MILLISECOND);

                return (hours * 3600 + minutes * 60 + seconds) * 1000 + millis;
            }

        private void doTimedAction()
            {
                try
                    {
                        performAction();
                    }
                catch (Throwable e)
                    {
                        logError("An error occured during timed execution.", e);
                    }
            }

        /**
         * This method will be called when an error or a warning occures.
         *
         * @param msg
         * @param e
         */
        protected void logError(String msg, Throwable e)
            {
                System.out.println(msg);
                e.printStackTrace();
            }

        /**
         * Action to be performed when scheduled time happens.
         */
        protected abstract void performAction();
    }

:

        TimeMonitor archiver = new TimeMonitor()
            {
                protected void performAction()
                    {
                        // Do the task
                    }
            };

        archiver.setTime(16.5); // Run at 4:30pm
        archiver.startTimer();
+1

. Spring .

+1

java.util.concurrent, . .

0

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


All Articles