{"id":724,"date":"2010-08-07T13:19:16","date_gmt":"2010-08-07T07:49:16","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=724"},"modified":"2010-08-07T13:19:16","modified_gmt":"2010-08-07T07:49:16","slug":"simple-threading-techniques","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/simple-threading-techniques\/","title":{"rendered":"Thread, Life Cycle of Thread"},"content":{"rendered":"<p style=\"text-align: left;\"><strong>Threading:<\/strong><\/p>\n<p>A process can contain one or more threads.<\/p>\n<p>In java by\u00a0default two threads will run i.e<strong>. main() &amp; garbage collection<\/strong>. we can create as many as threads as possible we want.<\/p>\n<p><strong>Benefit of threads:<\/strong><\/p>\n<p>We can delegate a long running tasks which does not require any i\/p from user to a thread resulting in fast execution of main thread(concentrated logic).<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Steps to create thread:<\/span><\/strong><\/p>\n<p><strong> Method 1:<\/strong><\/p>\n<ol>\n<li>Create a class which extends <strong>&#8220;Thread&#8221;\u009d<\/strong><\/li>\n<li>Override <strong>&#8220;run()&#8221;\u009d.<\/strong><\/li>\n<li>Create a object of a class which extends thread by using below line of code.<\/li>\n<\/ol>\n<p><em> Thread obj=new &lt;class which extends thread&gt;;<\/em><\/p>\n<p><strong> <\/strong> 4. \u00a0Call \u00a0method <strong>&#8220;start()&#8221;\u009d<\/strong>, internally it will call method <strong>&#8220;run()&#8221;\u009d.<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">Lifecycle of Thread:<\/span><\/strong><\/p>\n<p>Thread is a light weight process.<\/p>\n<p><strong> 1. New State:<\/strong><\/p>\n<p>After the creation of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.<\/p>\n<p><strong>2. Runnable State:<\/strong><\/p>\n<p>A \u00a0thread start its life from <strong>Runnable State<\/strong>. A thread first enters <strong>runnable state<\/strong> after the invoking of <strong>start()<\/strong> method but a thread can return to this state after <strong>either running, waiting, sleeping or coming back from blocked<\/strong> state also. On this state a thread is waiting for a turn on the process.<\/p>\n<p><strong>3. Running State:-<\/strong><\/p>\n<p>A thread is <strong>running state<\/strong> that means the thread is currently executing. There are several ways to enter in <strong>runnable\u00a0 state<\/strong> but there is only one way to enter <strong>in running state<\/strong>, the <strong>scheduler select a thread from runnable part.<\/strong><\/p>\n<p><strong>4. Dead State:-<\/strong><\/p>\n<p>A thread can be considered dead when its <strong>run()<\/strong> method completes. If any thread comes on this state that means it cannot ever run again.<\/p>\n<p><strong>5. Blocked:-<\/strong><\/p>\n<p>A thread can enter in this state because of <strong>waiting the<\/strong> <strong>resources <\/strong>that are <strong>hold by another thread<\/strong>.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\nclass \u00a0temp extends Thread\n{\n   public void run()\n   {\n      System.out.println(\"In run method\"\u009d);\n   }\n}\n\nclass \u00a0TestThread\n{\n   public static void main(String args&#x5B;])\n   {\n      Thread obj=new temp();\n      Obj.start();\n   }\n}\n<\/pre>\n<p><strong>Q. What is the difference between calling method start() &amp; calling method run() of thread?<\/strong><\/p>\n<p>i. <strong>start method<\/strong> of thread will <strong>internally <\/strong>create a <strong>new thread<\/strong> <strong>&amp; calls<\/strong> <strong>run method<\/strong>. The sequence of execution of thread in unpredictable.<\/p>\n<p>ii. <strong>run method<\/strong> will be <strong>called as a<\/strong> <strong>normal method in a main<\/strong> thread,if call externally.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\nclass Temp extends &lt;strong&gt;Thread&lt;\/strong&gt;\n{\n  public void run()\n  {\n     for(int i=0;i&lt;20;i++)\n     {\n         System.oput.println(\"In Run Method\"\u009d);\n     }\n  }\n}\n\nclass TestThread\n{\n   public static void main(String args&#x5B;])\n  {\n     Thread obj=new Temp();\n     Obj.start();\n     for(int i=0;i&lt;20;i++)\n     {\n           System.out.println(\"In Main Method\"\u009d);\n     }\n   }\n}\n\n<\/pre>\n<p><strong>O\/P:-<\/strong><\/p>\n<p><strong> <\/strong>In Main Method <span style=\"color: #339966;\">\/\/ will run fifteen times.<\/span><\/p>\n<p>In Run Method <span style=\"color: #339966;\">\/\/ will run twenty times.<\/span><\/p>\n<p>In Main Method <span style=\"color: #339966;\">\/\/ will run five times.<\/span><\/p>\n<p><strong> <\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Thread? Life cycle of thread. Difference in start() and run() method of Thread. By Default how many thread runs?<\/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":[180],"class_list":["post-724","post","type-post","status-publish","format-standard","hentry","category-java","tag-simple-threading-techniques"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1816,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/daemon-thread-java\/","url_meta":{"origin":724,"position":0},"title":"Daemon Thread","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"Example and Explanation of Daemon thread in JAVA","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":414,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/background-and-foreground-thread-in-c\/","url_meta":{"origin":724,"position":1},"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":757,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/anonymous-classimplements-interface-methods-of-threads\/","url_meta":{"origin":724,"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":1820,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-threading-executor-framework-and-callable-interface\/","url_meta":{"origin":724,"position":3},"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":[]},{"id":1825,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-thread-timertask\/","url_meta":{"origin":724,"position":4},"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":724,"position":5},"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":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/724","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=724"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/724\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}