{"id":10474,"date":"2014-06-16T21:34:30","date_gmt":"2014-06-16T18:34:30","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=10474"},"modified":"2014-06-17T00:12:11","modified_gmt":"2014-06-16T21:12:11","slug":"java-timer-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/","title":{"rendered":"Java Timer example"},"content":{"rendered":"<p>In this example, we will learn about the <code><a title=\"Timer\" href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Timer.html\" target=\"_blank\">Timer<\/a><\/code> class available under the <code><a title=\"java.util\" href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/package-summary.html\" target=\"_blank\">java.util<\/a><\/code> package.<\/p>\n<p>The <code>Timer<\/code> facilitates the execution of tasks in a background thread. The tasks to be executed by the <code>Timer<\/code> can be chosen either to be a one-time execution OR a repeated execution at pre-defined intervals.<\/p>\n<p>Along with the mentioned execution frequency of the task, the scheduling time \/ delay of these tasks can also be optionally mentioned to the <code>Timer<\/code> class.<\/p>\n<p>Representing what is stated above, the <code><a title=\"TimerTask\" href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/TimerTask.html\" target=\"_blank\">TimerTask<\/a><\/code> to be executed can be:<\/p>\n<ol>\n<li><b>One-time execution<\/b> which in turn can be:\n<ul>\n<li>Scheduled <b>immediately<\/b><\/li>\n<li>Scheduled to start <b>after a delay<\/b><\/li>\n<\/ul>\n<\/li>\n<li><b>Repeated execution<\/b> at regular intervals which in turn can be:\n<ul>\n<li>Scheduled <b>immediately<\/b><\/li>\n<li>Scheduled to start <b>after a delay<\/b><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2><span style=\"text-decoration: underline\">Components of Timer<\/span><\/h2>\n<p>We would briefly look at the internal components of the <code>Timer<\/code> class. In terms of usage, one only needs to create Tasks by extending the <code><a title=\"TimerTask\" href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/TimerTask.html\" target=\"_blank\">TimerTask<\/a><\/code> and schedule them with the <code>Timer<\/code> instance.<\/p>\n<p><i>The other two internal components &#8211; <code>TaskQueue<\/code> and <code>TimerThread<\/code> have been mentioned below for information purpose, so as to help one evaluate when and for what kind of tasks should the <code>Timer<\/code> class be used.<\/i><\/p>\n<ol>\n<li>\n<h3>Task Queue<\/h3>\n<\/li>\n<p>Internally, the <code>Timer<\/code> uses the <code>TaskQueue<\/code> as a &#8220;priority&#8221; queue maintaining tasks in the order of next execution. The priority queue holds task in the sequence they should be executed by the <code>TimerThread<\/code>.<\/p>\n<li>\n<h3>Timer Thread<\/h3>\n<\/li>\n<p>The <code>TimerThread<\/code> is a thread helper class which repeatedly monitors the <code>TaskQueue<\/code> and &#8220;sequentially&#8221; executes the tasks based on their execution order and time of execution. To note here, the <code>Timer<\/code> has only <b>one instance of the <code>TimerThread<\/code><\/b> internally. Thus, if any of the scheduled <code>Timer<\/code> tasks takes excessive time to run, the other tasks scheduled for execution will keep waiting till the offending task completes. The waiting tasks may then be executed in rapid successions by the <code>TimerThread<\/code> causing unexpected results.<\/p>\n<li>\n<h3>Timer Task<\/h3>\n<\/li>\n<p>The <code><a title=\"TimerTask\" href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/TimerTask.html\" target=\"_blank\">TimerTask<\/a><\/code> is the actual unit of task that needs to be performed by the <code>Timer<\/code> for either once or multiple times at regular intervals. The <code>TimerTask<\/code> is an abstract class implementing Runnable. <b>As a user of the <code>Timer<\/code>, one needs to extend this class and implement the run method by providing the logic of the task that needs to be performed.<\/b>\n<\/ol>\n<p>Now, let us look at an example using the <code>Timer<\/code>. In the example, we create two <code>TimerTask<\/code>:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<ol>\n<li>A <code>TimerTask<\/code> in a slave node which sends some heart-beat information at an interval of 10 seconds. The task is scheduled to start with a delay of 1 second.<\/li>\n<li>A <code>TimerTask<\/code> which updates the status being sent by the first task to AMBER. This task is executed once after a delay of 30 seconds.<\/li>\n<\/ol>\n<p>Let&#8217;s look at <code>TimerTask<\/code> #1, <code>TimerTaskSendHeartBeat<\/code> for heartbeat sending.<\/p>\n<pre class=\"brush:java\">class TimerTaskSendHeartBeat extends TimerTask {\r\n\r\n\tTimerExample healthStatusHolder = null;\r\n\r\n\tpublic TimerTaskSendHeartBeat(TimerExample healthStatusHolder) {\r\n\t\tthis.healthStatusHolder = healthStatusHolder;\r\n\t}\r\n\r\n\tHeartBeatMessage message = null;\r\n\r\n\t@Override\r\n\tpublic void run() {\r\n\r\n\t\t\/\/ create HeartBeat message by getting Health Status (RED\/GREEN\/AMBER)\r\n\t\t\/\/ Error Code, if any AND time at which heartbeat is sent to help\r\n\t\t\/\/ receiver discard any delayed messages due to latency\r\n\t\tmessage = new HeartBeatMessage(\r\n\t\t\t\tthis.healthStatusHolder.getHealthStatus(), Calendar\r\n\t\t\t\t\t\t.getInstance().getTimeInMillis(), -1);\r\n\r\n\t\tSystem.out.println(\"Sending HeartBeat Message\");\r\n\r\n\t\t\/\/ Send the message to Monitoring Dashboard\r\n\t\tSystem.out.println(message);\r\n\r\n\t\tSystem.out.println(\"HeartBeat Message Sent\");\r\n\t}\r\n\r\n\t\/**\r\n\t * Simple POJO which is a heartbeat message object It can have any decoder\r\n\t * encoder mechanism to send over any messaging platform\r\n\t *\/\r\n\tclass HeartBeatMessage {\r\n\t\tprivate String status;\r\n\t\tprivate long heartBeatTime;\r\n\t\tprivate int errorCode;\r\n\r\n\t\tpublic HeartBeatMessage(String status, long heartBeatTime, int errorCode) {\r\n\t\t\tthis.status = status;\r\n\t\t\tthis.heartBeatTime = heartBeatTime;\r\n\t\t\tthis.errorCode = errorCode;\r\n\t\t}\r\n\r\n\t\tpublic String getStatus() {\r\n\t\t\treturn status;\r\n\t\t}\r\n\r\n\t\tpublic long getHeartBeatTime() {\r\n\t\t\treturn heartBeatTime;\r\n\t\t}\r\n\r\n\t\tpublic int getErrorCode() {\r\n\t\t\treturn errorCode;\r\n\t\t}\r\n\r\n\t\t@Override\r\n\t\tpublic String toString() {\r\n\t\t\treturn \"status: \" + status + \" timeOfHeartBeat: \"\r\n\t\t\t\t\t+ new java.util.Date(this.heartBeatTime) + \" errCode : \"\r\n\t\t\t\t\t+ this.errorCode;\r\n\t\t}\r\n\t}\r\n<\/pre>\n<p>Below is the implementation of the <code>TimerTask<\/code> # 2, <code>TimerTaskUpdateHeartBeat<\/code> which just updates the status of the heartbeat message being sent.<\/p>\n<pre class=\"brush:java\">\r\nclass TimerTaskUpdateHeartBeat extends TimerTask {\r\n\r\n\tTimerExample healthClass = null;\r\n\r\n\tpublic TimerTaskUpdateHeartBeat(TimerExample healthClass) {\r\n\t\tthis.healthClass = healthClass;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void run() {\r\n\t\tSystem.out.println(\"Task 2:: 30 seconds completed :: Updating health \"\r\n\t\t\t\t+ \"status to AMBER\");\r\n\t\thealthClass.setHealthStatus(\"AMBER\");\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>Once both the <code>TimerTask<\/code> have been created, let us know schedule the tasks at the desired time and frequency of execution. This would require creating a new <code>Timer<\/code> instance and scheduling tasks to it.<\/p>\n<pre class=\"brush:java\">public class TimerExample {\r\n\tprivate String healthStatus = \"GREEN\";\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\tTimerExample example = new TimerExample();\r\n\t\texample.setHealthStatus(\"GREEN\");\r\n\r\n\t\t\/\/ Create the Timer object\r\n\t\tTimer timer = new Timer(\"JCG Timer Example\");\r\n\r\n\t\t\/\/ Create Timer task created to send heartBeats\r\n\t\tTimerTask taskToExecute = new TimerTaskSendHeartBeat(example);\r\n\t\t\/\/ schedule the task to start executing after 1 second\r\n\t\t\/\/ and re-execute every 10 seconds\r\n\t\ttimer.scheduleAtFixedRate(taskToExecute, 1000, 10000);\r\n\r\n\t\t\/\/ Create Timer task to setHeartBeatStatus\r\n\t\tTimerTask setHeartBeatStatus = new TimerTaskUpdateHeartBeat(example);\r\n\t\t\/\/ schedule the task to start immediately but execute\r\n\t\t\/\/ first time after 30 seconds\r\n\t\ttimer.schedule(setHeartBeatStatus, 30000);\r\n\r\n\t\t\/\/ Wait for 60 seconds and then cancel the timer cleanly\r\n\t\ttry {\r\n\t\t\tThread.sleep(60000);\r\n\t\t} catch (InterruptedException e) {\r\n\t\t}\r\n\t\tSystem.out.println(\"Cancelling Timer Cleanly after 60 seconds\");\r\n\t\ttimer.cancel();\r\n\t}\r\n\r\n\t\/**\r\n\t * Get Heartbeat Status of the application, could be GREEN \/ AMBER \/ RED\r\n\t * based on any exceptions or service health\r\n\t * \r\n\t * @return String\r\n\t *\/\r\n\tpublic String getHealthStatus() {\r\n\t\treturn this.healthStatus;\r\n\t}\r\n\r\n\t\/**\r\n\t * Set the status for the application could be GREEN \/ AMBER \/ RED\r\n\t * \r\n\t * @param healthStatus\r\n\t *\/\r\n\tpublic void setHealthStatus(String healthStatus) {\r\n\t\tthis.healthStatus = healthStatus;\r\n\t}\r\n}\r\n<\/pre>\n<p>On execution of the program, the output looks as below:<\/p>\n<pre class=\"brush:bash\">\r\nSending HeartBeat Message\r\nstatus: GREEN timeOfHeartBeat: Mon Jun 16 23:52:04 IST 2014 errCode : -1\r\nHeartBeat Message Sent\r\nSending HeartBeat Message\r\nstatus: GREEN timeOfHeartBeat: Mon Jun 16 23:52:14 IST 2014 errCode : -1\r\nHeartBeat Message Sent\r\nSending HeartBeat Message\r\nstatus: GREEN timeOfHeartBeat: Mon Jun 16 23:52:24 IST 2014 errCode : -1\r\nHeartBeat Message Sent\r\nTask 2:: 30 seconds completed :: Updating health status to AMBER\r\nSending HeartBeat Message\r\nstatus: AMBER timeOfHeartBeat: Mon Jun 16 23:52:34 IST 2014 errCode : -1\r\nHeartBeat Message Sent\r\nSending HeartBeat Message\r\nstatus: AMBER timeOfHeartBeat: Mon Jun 16 23:52:44 IST 2014 errCode : -1\r\nHeartBeat Message Sent\r\nSending HeartBeat Message\r\nstatus: AMBER timeOfHeartBeat: Mon Jun 16 23:52:54 IST 2014 errCode : -1\r\nHeartBeat Message Sent\r\nCancelling Timer Cleanly after 60 seconds\r\n<\/pre>\n<p>As seen from output above, the <code>Timer<\/code> is cancelled using the <code>cancel<\/code> method which ensures that any <code>TimerTask<\/code> being executed is completed before the <code>Timer<\/code> is cleaned up.<\/p>\n<h2>Other Points about Timer<\/h2>\n<ul>\n<li>The <code>Timer<\/code> instance created can be instructed to start the <code>TaskThread<\/code> as a Daemon Thread, in case the thread should no longer exist if there are no non-daemon threads remaining in the VM.<\/li>\n<li>The <code>Timer<\/code> is thread-safe and is internally synchronized.<\/li>\n<li>Java 5.0 introduced the <code><a title=\"ScheduledThreadPoolExecutor\" href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/concurrent\/ScheduledThreadPoolExecutor.html\" target=\"_blank\">ScheduledThreadPoolExecutor<\/a><\/code> which is a thread pool for repeatedly executing tasks at a given rate or delay. It allows multiple service threads and accepts various time units. Thus, as the <code>Timer<\/code> has one single task execution thread (highlighted above), the <code>ScheduledThreadPoolExecutor<\/code> can have multiple threads executing\/dedicated to task execution, in turn preventing one faulty task causing other tasks to be waiting for thread resource for execution.<\/li>\n<\/ul>\n<p>The <b>source code<\/b> is available for download <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/06\/TimerJCGExample.zip\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this example, we will learn about the Timer class available under the java.util package. The Timer facilitates the execution of tasks in a background thread. The tasks to be executed by the Timer can be chosen either to be a one-time execution OR a repeated execution at pre-defined intervals. Along with the mentioned execution &hellip;<\/p>\n","protected":false},"author":20,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[304],"tags":[1168,1108],"class_list":["post-10474","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-timer-util","tag-scheduledthreadpoolexecutor","tag-timertask"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Timer example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this example, we will learn about the Timer class available under the java.util package. The Timer facilitates the execution of tasks in a background\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Timer example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this example, we will learn about the Timer class available under the java.util package. The Timer facilitates the execution of tasks in a background\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-16T18:34:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-06-16T21:12:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Vishal Rajpal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishal Rajpal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/\"},\"author\":{\"name\":\"Vishal Rajpal\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/12ed9c93288f688dbdbdcb1c46063530\"},\"headline\":\"Java Timer example\",\"datePublished\":\"2014-06-16T18:34:30+00:00\",\"dateModified\":\"2014-06-16T21:12:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/\"},\"wordCount\":622,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"keywords\":[\"ScheduledThreadPoolExecutor\",\"TimerTask\"],\"articleSection\":[\"Timer\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/\",\"name\":\"Java Timer example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2014-06-16T18:34:30+00:00\",\"dateModified\":\"2014-06-16T21:12:11+00:00\",\"description\":\"In this example, we will learn about the Timer class available under the java.util package. The Timer facilitates the execution of tasks in a background\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"Bipartite Graph\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"util\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Timer\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/timer-util\/\"},{\"@type\":\"ListItem\",\"position\":6,\"name\":\"Java Timer example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/12ed9c93288f688dbdbdcb1c46063530\",\"name\":\"Vishal Rajpal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/06\/Vishal-Rajpal-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/06\/Vishal-Rajpal-96x96.jpg\",\"caption\":\"Vishal Rajpal\"},\"description\":\"Vishal enjoys designing and developing small and mid-sized Enterprise solutions in Java and it's ecosystem. He has graduated from RGTU, India in Electronics and Telecommunication Engineering. Currently, Vishal is working with an Analytics organization, enabling the in-house analytics as well as client teams to use technology as a differentiator for projects \/ problems related to Big Data.\",\"sameAs\":[\"http:\/\/www.javacodegeeks.com\/\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/vishal-rajpal\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Timer example - Java Code Geeks","description":"In this example, we will learn about the Timer class available under the java.util package. The Timer facilitates the execution of tasks in a background","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/","og_locale":"en_US","og_type":"article","og_title":"Java Timer example - Java Code Geeks","og_description":"In this example, we will learn about the Timer class available under the java.util package. The Timer facilitates the execution of tasks in a background","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2014-06-16T18:34:30+00:00","article_modified_time":"2014-06-16T21:12:11+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Vishal Rajpal","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Vishal Rajpal","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/"},"author":{"name":"Vishal Rajpal","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/12ed9c93288f688dbdbdcb1c46063530"},"headline":"Java Timer example","datePublished":"2014-06-16T18:34:30+00:00","dateModified":"2014-06-16T21:12:11+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/"},"wordCount":622,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["ScheduledThreadPoolExecutor","TimerTask"],"articleSection":["Timer"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/","name":"Java Timer example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2014-06-16T18:34:30+00:00","dateModified":"2014-06-16T21:12:11+00:00","description":"In this example, we will learn about the Timer class available under the java.util package. The Timer facilitates the execution of tasks in a background","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/timer-util\/java-timer-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"util","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/"},{"@type":"ListItem","position":5,"name":"Timer","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/timer-util\/"},{"@type":"ListItem","position":6,"name":"Java Timer example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/12ed9c93288f688dbdbdcb1c46063530","name":"Vishal Rajpal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/06\/Vishal-Rajpal-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/06\/Vishal-Rajpal-96x96.jpg","caption":"Vishal Rajpal"},"description":"Vishal enjoys designing and developing small and mid-sized Enterprise solutions in Java and it's ecosystem. He has graduated from RGTU, India in Electronics and Telecommunication Engineering. Currently, Vishal is working with an Analytics organization, enabling the in-house analytics as well as client teams to use technology as a differentiator for projects \/ problems related to Big Data.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/examples.javacodegeeks.com\/author\/vishal-rajpal\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/10474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=10474"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/10474\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=10474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=10474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=10474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}