{"id":1816,"date":"2011-03-24T15:45:16","date_gmt":"2011-03-24T10:15:16","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1816"},"modified":"2011-03-24T15:45:16","modified_gmt":"2011-03-24T10:15:16","slug":"daemon-thread-java","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/daemon-thread-java\/","title":{"rendered":"Daemon Thread"},"content":{"rendered":"<p>In java we have two types of Threads: <strong>Daemon Thread<\/strong> and<strong> User Threads<\/strong>. Generally all threads created by programmer are user thread (unless you specify it to be daemon or your parent thread is a daemon thread).<\/p>\n<p>User thread is generally meant to run our program code. <strong>JVM doesn&#8217;t terminate unless all the user thread terminate.<\/strong><br \/>\nOn the other hand we have Daemon threads. Typically these threads are <strong>service provider threads<\/strong>. They should not be used to run your program code but some system code. These threads run parallel to your code but survive on the mercy of the JVM. When JVM finds no user threads it stops and all daemon thread terminate instantly. Thus one should never rely on daemon code to perform any program code.<!--more--><\/p>\n<p>For better understanding consider a well known example of Daemon thread: <strong>Java Garbage collector<\/strong>. Garbage collector runs as a daemon thread to reclaim any unused memory. When all user threads terminates, JVM may stop and garbage collector also terminates instantly.<\/p>\n<p>If only daemon threads are active in the application, then JVM quit the program (At least one user thread is necessary for application to run in JVM).<br \/>\n<strong>setDaemon(boolean)<\/strong> method is used to mark any thread as daemon thread.<\/p>\n<pre class=\"brush: java; highlight: [8]; title: ; notranslate\" title=\"\">\npackage com.G2.Thread;\n\npublic class DaemonThreadDemo extends Thread {\n\n\tpublic static void main(String&#x5B;] args) {\n\t\tThread t = new DaemonThreadDemo();\n                \/\/ Set Thread to daemon\n\t\tt.setDaemon(true);\n\t\tt.start();\n\t}\n\n\tpublic void run(){\n\t\tfor(int i=1;i&lt;1000;i++){\n\t\t\tSystem.out.println(i);\n\t\t}\n\t}\n}\n<\/pre>\n<p>In above program, if &#8220;<strong>t.setDaemon(true)<\/strong>&#8220;\u009d statement is commented then program prints the number from 1 to 999. It is the normal execution. But when we uncomment that statement, it means that the main Thread is only User thread and &#8220;t&#8221;\u009d is daemon thread. And therefore the number will not be printed completely. As only daemon thread lasts, the JVM exits the program before completion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Example and Explanation of Daemon thread in JAVA<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3],"tags":[198],"class_list":["post-1816","post","type-post","status-publish","format-standard","hentry","category-java","tag-thread"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":414,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/background-and-foreground-thread-in-c\/","url_meta":{"origin":1816,"position":0},"title":"Background and foreground thread in c#","author":"Jitendra","date":"June 29, 2010","format":false,"excerpt":"Example of Threading in C#. Includes Foreground and Background Threading.","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"Thread Foreground Background","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/06\/ThreadDemo-300x93.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":724,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/simple-threading-techniques\/","url_meta":{"origin":1816,"position":1},"title":"Thread, Life Cycle of Thread","author":"Jitendra","date":"August 7, 2010","format":false,"excerpt":"What is Thread? Life cycle of thread. Difference in start() and run() method of Thread. By Default how many thread runs?","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":757,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/anonymous-classimplements-interface-methods-of-threads\/","url_meta":{"origin":1816,"position":2},"title":"Create Thread using Anonymous class and Interface","author":"Jitendra","date":"August 16, 2010","format":false,"excerpt":"Demonstration of creating Thread by Anonymous class and Interface","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1825,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-thread-timertask\/","url_meta":{"origin":1816,"position":3},"title":"Java Thread &#8211; Executor framework, Timer and TimerTask","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"Tutorial and example of Executor framework, Timer and TimerTask over Thread","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Animation of ProgressBar in Swing Using TimerTask","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Animation-of-ProgressBar-Swing-Using-TimerTask.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":748,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/producer-consumer-problem\/","url_meta":{"origin":1816,"position":4},"title":"Thread Synchronization","author":"Jitendra","date":"August 7, 2010","format":false,"excerpt":"Describes Synchronization, Producer and Consumer Problem, wait(), notify(), InterThread Communication","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1820,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-threading-executor-framework-and-callable-interface\/","url_meta":{"origin":1816,"position":5},"title":"Java Threading &#8211; Executor Framework and Callable Interface","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"Java Threading - Executor Framework and Callable Interface with example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1816"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1816\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}