{"id":3670,"date":"2014-03-29T07:46:28","date_gmt":"2014-03-29T07:46:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/i-need-to-sleep-for-long-periods-days-in-java-collection-of-common-programming-errors\/"},"modified":"2014-03-29T07:46:28","modified_gmt":"2014-03-29T07:46:28","slug":"i-need-to-sleep-for-long-periods-days-in-java-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/i-need-to-sleep-for-long-periods-days-in-java-collection-of-common-programming-errors\/","title":{"rendered":"I need to sleep for long periods (days) in Java-Collection of common programming errors"},"content":{"rendered":"<p>Well, you can schedule a cron job if you need your app to run at certain times. Or, you can write a simple class that will schedule itself for certain times. Since I&#8217;m not aware of any library out there (never really needed to look for one), I wrote a class long ago to schedule a task for myself. Hope it helps and in ballpark:<\/p>\n<pre><code>import java.util.*;\n\npublic abstract class TimeMonitor extends Thread\n    {\n        protected double execution_time;\n        protected boolean execute_at_startup;\n\n        public TimeMonitor()\n            {\n                execute_at_startup = false;\n            }\n\n        public TimeMonitor(double time)\n            {\n                execution_time = time;\n                execute_at_startup = false;\n            }\n\n        public void startTimer()\n            {\n                setPriority(Thread.MIN_PRIORITY);\n                start();\n            }\n\n        public void setTime(double time)\n            {\n                execution_time = time;\n            }\n\n        public void executeAtStartup()\n            {\n                execute_at_startup = true;\n            }\n\n        public void run()\n            {\n                if (execute_at_startup)\n                    doTimedAction();\n\n                while (true)\n                    {\n                        long runtime = (long)(execution_time * 3600 * 1000);\n                        long now     = getTime();\n                        long sleep   = 0;\n\n                        \/\/ Calculate how long to way until first run\n                        if (runtime &gt; now)\n                            sleep = runtime - now;\n                        else\n                            sleep = 24 * 3600 * 1000 - now + runtime;\n\n                        try\n                            {\n                                Thread.sleep(sleep);\n                            }\n                        catch (InterruptedException e)\n                            {\n                                logError(\"Wait thread has been interrupted.\", e);\n                                continue;\n                            }\n\n                        doTimedAction();\n                    }\n            }\n\n        \/**\n         * Calculates number of milliseconds from start of the day.\n         *\n         * @return Number of milliseconds.\n         *\/\n        private long getTime()\n            {\n                Calendar cal = Calendar.getInstance();\n                int hours   = cal.get(Calendar.HOUR_OF_DAY);\n                int minutes = cal.get(Calendar.MINUTE);\n                int seconds = cal.get(Calendar.SECOND);\n                int millis  = cal.get(Calendar.MILLISECOND);\n\n                return (hours * 3600 + minutes * 60 + seconds) * 1000 + millis;\n            }\n\n        private void doTimedAction()\n            {\n                try\n                    {\n                        performAction();\n                    }\n                catch (Throwable e)\n                    {\n                        logError(\"An error occured during timed execution.\", e);\n                    }\n            }\n\n        \/**\n         * This method will be called when an error or a warning occures.\n         *\n         * @param msg\n         * @param e\n         *\/\n        protected void logError(String msg, Throwable e)\n            {\n                System.out.println(msg);\n                e.printStackTrace();\n            }\n\n        \/**\n         * Action to be performed when scheduled time happens.\n         *\/\n        protected abstract void performAction();\n    }\n<\/code><\/pre>\n<p>Extend to use it like so:<\/p>\n<pre><code>        TimeMonitor archiver = new TimeMonitor()\n            {\n                protected void performAction()\n                    {\n                        \/\/ Do the task\n                    }\n            };\n\n        archiver.setTime(16.5); \/\/ Run at 4:30pm\n        archiver.startTimer();\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Well, you can schedule a cron job if you need your app to run at certain times. Or, you can write a simple class that will schedule itself for certain times. Since I&#8217;m not aware of any library out there (never really needed to look for one), I wrote a class long ago to schedule [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3670","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3670"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3670\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}