{"id":25523397,"date":"2022-05-08T05:07:19","date_gmt":"2022-05-07T23:37:19","guid":{"rendered":"https:\/\/entri.app\/blog\/?p=25523397"},"modified":"2022-11-23T20:21:59","modified_gmt":"2022-11-23T14:51:59","slug":"understanding-java-threads-all-about-java-threads","status":"publish","type":"post","link":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/","title":{"rendered":"What is a thread in Java- All About Java Threads"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_79_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69d676ff43379\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69d676ff43379\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#Define_thread_in_Java\" >Define thread in Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#Thread_Life_Cycle\" >Thread Life Cycle<\/a><\/li><\/ul><\/nav><\/div>\n<div class=\"jb jc jd je jf\">\n<div class=\"\">\n<h2 id=\"f373\" class=\"mx kv ji bn kw my mz na la nb nc nd le ne nf ng li nh ni nj lm nk nl nm lq nn gc\"><span class=\"ez-toc-section\" id=\"Define_thread_in_Java\"><\/span><strong>Define thread in Java<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<\/div>\n<p id=\"c2aa\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">Thread is a\u00a0flow of execution\u00a0through the process code and the process can contain multiple threads which is known as\u00a0<strong class=\"lu jj\">Multi-threading.<\/strong> Basically its a multiple flow of execution. So running multiple task at a certain time. This is where most of the people misunderstood, let\u2019s say there is a process which runs for 10 minutes and there is no guarantee that creating ten threads for this process will cut down the time to one minute, because there maybe dependent tasks that can be run in a program. But time can be cut down up to 7 or 8 seconds and it depends on the process. Threads used to perform complicated tasks in the background without disturbing the main process.<\/p>\n<figure class=\"mo mp mq mr gz kj gn go paragraph-image\">\n<div class=\"gn go mn\"><img loading=\"lazy\" decoding=\"async\" class=\"cf ko kp\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/max\/562\/1*L2hjJPJHZlTOs07L-Wuubw.jpeg\" alt=\"\" width=\"281\" height=\"181\" \/><\/div><figcaption class=\"kq bm gp gn go kr ks bn b bo bp co\" data-selectable-paragraph=\"\">Multitasking<\/figcaption><\/figure>\n<p id=\"0e36\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">There are 2 type of multi-threading as shown in the above diagram. In Process based, lets say for example, programmers usually listen to music while coding and sometimes they may be downloading any documents from the internet, which means, they are doing multiple things on the same time. These tasks are independent, it won\u2019t affect any of them if one of the above processes were closed. In Thread based multitasking, each and every threads are belongs to a same main process. Usually a single main thread will be divided into multiple tasks. lets say there are 100 files to be processed for 100 minutes. By using two threads, these process might be finished in less than 100 minutes. But there is no way by increasing the number of threads will help to complete the task in 1 second or in no time. Because there is no point in taking a same file and processing again and again. This explanation is just for the clarification not the exact real life scenario and its just a basic idea of what is a thread and their limitations.<\/p>\n<h3><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-25494072 size-full\" src=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square.png\" alt=\"\" width=\"345\" height=\"345\" srcset=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square.png 345w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square-300x300.png 300w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square-150x150.png 150w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square-24x24.png 24w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square-48x48.png 48w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square-96x96.png 96w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square-75x75.png 75w\" sizes=\"auto, (max-width: 345px) 100vw, 345px\" \/><\/a><\/h3>\n<h2 id=\"f373\" class=\"mx kv ji bn kw my mz na la nb nc nd le ne nf ng li nh ni nj lm nk nl nm lq nn gc\"><span class=\"ez-toc-section\" id=\"Thread_Life_Cycle\"><\/span><strong>Thread Life Cycle<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<figure class=\"mo mp mq mr gz kj gn go paragraph-image\">\n<div class=\"kk kl dq km cf kn\" role=\"button\">\n<div class=\"gn go no\">In the life cycle, there are 7 states. lets discuss about them,<\/div>\n<\/div>\n<\/figure>\n<h3 id=\"71d8\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>New<\/strong><\/h3>\n<p id=\"633b\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">This the state where the thread is created.<\/p>\n<h3 id=\"7373\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Ready\/Runnable<\/strong><\/h3>\n<p id=\"e971\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">When the\u00a0<code class=\"fr np nq nr ns b\">start()<\/code>\u00a0method is invoked to a particular thread, it will switch state from\u00a0<strong class=\"lu jj\">New\u00a0<\/strong>to\u00a0<strong class=\"lu jj\">Ready or Runnable\u00a0<\/strong>state<\/p>\n<h3 id=\"7480\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Running<\/strong><\/h3>\n<p id=\"5861\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">The Thread will switch to Running state when\u00a0<code class=\"fr np nq nr ns b\">run()<\/code>\u00a0method is invoked. which means when the process is executing. But it may go back to Ready\/Runnable state and come back to Running state and this can happen again and again also.<\/p>\n<h3 id=\"9bf5\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Blocked<\/strong><\/h3>\n<p id=\"6236\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">This is the state where one thread blocked on the lock because other thread has already acquired this lock.<\/p>\n<h3 id=\"fad6\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Waiting<\/strong><\/h3>\n<p id=\"99c4\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">In this state, the thread will wait forever until if there is any interruption. Usually the invocation of\u00a0<code class=\"fr np nq nr ns b\">join()<\/code>\u00a0or\u00a0<code class=\"fr np nq nr ns b\">wait()<\/code>\u00a0method will put the thread in waiting state.<\/p>\n<h3 id=\"6242\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Timed Waiting<\/strong><\/h3>\n<p id=\"3484\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">When\u00a0<code class=\"fr np nq nr ns b\">sleep()<\/code>\u00a0method or\u00a0<code class=\"fr np nq nr ns b\">join()<\/code>\u00a0or\u00a0<code class=\"fr np nq nr ns b\">wait()<\/code>\u00a0methods with timeout are invoked, that state is known as\u00a0<strong class=\"lu jj\">Timed Waiting.\u00a0<\/strong>Name itself explains that the the thread will wait for a certain given time.<\/p>\n<h3 id=\"a7a6\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Dead<\/strong><\/h3>\n<p id=\"30db\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">This state represents the completion of process.<\/p>\n<h4 style=\"text-align: center;\"><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\"><strong>Learn to code from industry experts! Enroll here<\/strong><\/a><\/h4>\n<\/div>\n<h3><strong style=\"font-size: 1.953em;\">How to create a Thread in Java ?<\/strong><\/h3>\n<div class=\"jb jc jd je jf\">\n<p class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">In Java <span class=\"css-a5m6co-text css-p8ym46-fontFamily css-11397xj-fontSize css-18j1nfb-display\">thread can be created by two ways,<\/span><\/p>\n<ul class=\"\">\n<li id=\"c487\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm ok ol om on gc\" data-selectable-paragraph=\"\">By Extend Thread class<\/li>\n<li id=\"9bb9\" class=\"of og ji lu b lv oo lz op lf oq lj or ln os mm ok ol om on gc\" data-selectable-paragraph=\"\">By Implement Runnable Interface<\/li>\n<\/ul>\n<p id=\"cab0\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">lets check it one by one,<\/p>\n<h3 id=\"4269\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Extending Thread class<\/strong><\/h3>\n<p id=\"ddce\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">Here,\u00a0 I am going to use 2 classes. So to create a thread, we need to extend the Printer class with the Thread class as shown in the code below.<\/p>\n<p id=\"701f\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">Now to run the thread, we have to create an instance of the Thread class in the main method of the Application class and invoke that\u00a0<code class=\"fr np nq nr ns b\">start()<\/code>\u00a0to that instance as I mentioned in the code below.<\/p>\n<figure class=\"mo mp mq mr gz kj\">\n<div class=\"m l dq\">\n<div class=\"aei ou l\"><\/div>\n<\/div>\n<\/figure>\n<p id=\"ff06\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">So If I run the application I will get the output as,<\/p>\n<pre class=\"mo mp mq mr gz ov bt ow\"><span id=\"7cde\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Main thread is running....\r\nChild thread is running....<\/span><\/pre>\n<h3 id=\"2a00\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Facts about creating thread by Extending Thread class<\/strong><\/h3>\n<ul class=\"\">\n<li id=\"f708\" class=\"of og ji lu b lv lw lz ma lf pa lj pb ln pc mm ok ol om on gc\" data-selectable-paragraph=\"\">It is not must to override the run method in Thread class. Lets see why, when I call the\u00a0<code class=\"fr np nq nr ns b\">start()<\/code>\u00a0it will check the Printer thread. Since I don\u2019t have the\u00a0<code class=\"fr np nq nr ns b\">start()<\/code>method there, it will check the parent class witch is Thread class. In Thread class there will be a\u00a0<code class=\"fr np nq nr ns b\">start()<\/code>\u00a0and that invokes\u00a0<code class=\"fr np nq nr ns b\">run()<\/code>\u00a0method. So when calling run method it will the printer Thread class, since it doesn\u2019t has the\u00a0<code class=\"fr np nq nr ns b\">run()<\/code>\u00a0method, it will check the Thread class and it doesn\u2019t do anything. Inside that run method, if the target is set then it will execute\u00a0<code class=\"fr np nq nr ns b\">target.run()<\/code>\u00a0method. In our case we don\u2019t have any target so it\u2019s just don\u2019t do anything. thats the reason it worked.<br \/>\nSo If we are not overriding the run method which means we are not doing any tasks, because all the process of threads have to go to\u00a0<code class=\"fr np nq nr ns b\">run()<\/code>\u00a0method. This is only valid when we are Extend the Thread class. But when we implement the runnable interface, java program will force you to override the\u00a0<code class=\"fr np nq nr ns b\">run()<\/code> method. Because that&#8217;s the behavior of the runnable interface which we are going to look next.<\/li>\n<li id=\"a678\" class=\"of og ji lu b lv oo lz op lf oq lj or ln os mm ok ol om on gc\" data-selectable-paragraph=\"\">As you can see from the above code snippet, even though the\u00a0<code class=\"fr np nq nr ns b\">start()<\/code>\u00a0method invoked(in line number 7 on the Application class) before printing the sentence in the main thread, it was printed after the main thread prints. which means, there is no guarantee that invoking\u00a0<code class=\"fr np nq nr ns b\">start()<\/code>\u00a0method will immediately run the thread and it is entirely depend on the JRE(based on the OS). In the previous point, I mentioned about\u00a0<code class=\"fr np nq nr ns b\">target<\/code>\u00a0which is a runnable object. In our case printer object, so if it has\u00a0<code class=\"fr np nq nr ns b\">run()<\/code>\u00a0method then it will execute it and if not it will got the super class and execute the\u00a0<code class=\"fr np nq nr ns b\">run()<\/code>\u00a0method as I said before.<br \/>\nlets see some example of thread,<\/li>\n<\/ul>\n<p id=\"997b\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">so in this child thread this should print from 1 to 10.<\/p>\n<figure class=\"mo mp mq mr gz kj\">\n<div class=\"m l dq\">\n<div class=\"aei ou l\"><\/div>\n<\/div>\n<\/figure>\n<p id=\"687e\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">and in the main thread this should print from 1 to 100<\/p>\n<figure class=\"mo mp mq mr gz kj\">\n<div class=\"m l dq\">\n<div class=\"aej ou l\"><\/div>\n<\/div>\n<\/figure>\n<p id=\"ab7d\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">So I run this program several time to test the result.<\/p>\n<blockquote class=\"pd\">\n<p id=\"279d\" class=\"pe pf ji bn pg ph pi pj pk pl pm mm co\" data-selectable-paragraph=\"\">1st time output<\/p>\n<\/blockquote>\n<pre class=\"pn po pp pq pr ov bt ow\"><span id=\"dbf5\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Main thread is running....\r\nChild thread is running....\r\nmain 0\r\nchild 0\r\nchild 1\r\nmain 1\r\nmain 2\r\nmain 3\r\nchild 2\r\nmain 4\r\nchild 3\r\nmain 5\r\nchild 4\r\nmain 6\r\n.\r\n.<\/span><\/pre>\n<blockquote class=\"pd\">\n<p id=\"9cb4\" class=\"pe pf ji bn pg ph ps pt pu pv pw mm co\" data-selectable-paragraph=\"\">2nd time output<\/p>\n<\/blockquote>\n<pre class=\"pn po pp pq pr ov bt ow\"><span id=\"37eb\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Main thread is running....\r\nChild thread is running....\r\nchild 0\r\nmain 0\r\nmain 1\r\nmain 2\r\nchild 1\r\nmain 3\r\nchild 2\r\nmain 4\r\nchild 3\r\nmain 5\r\nchild 4\r\nmain 6\r\n.\r\n.<\/span><\/pre>\n<blockquote class=\"pd\">\n<p id=\"43c9\" class=\"pe pf ji bn pg ph ps pt pu pv pw mm co\" data-selectable-paragraph=\"\">3rd time output<\/p>\n<\/blockquote>\n<pre class=\"pn po pp pq pr ov bt ow\"><span id=\"8c85\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Main thread is running....\r\nChild thread is running....\r\nchild 0\r\nmain 0\r\nchild 1\r\nmain 1\r\nchild 2\r\nmain 2\r\nchild 3\r\nmain 3\r\nchild 4\r\nmain 4\r\nchild 5\r\nmain 5\r\nchild 6\r\nmain 6\r\n.\r\n.<\/span><\/pre>\n<p id=\"e03c\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">So from the above results, each execution has different orders, so this gave a conclusion, that there is no guarantee the thread will start immediate whenever the\u00a0<code class=\"fr np nq nr ns b\">start()<\/code>\u00a0method is invoked. Its because of the\u00a0<strong class=\"lu jj\">Thread Scheduler\u00a0<\/strong>and this decides which thread should run (order) and its entirely depends on JRE (OS based) as I mentioned above.<\/p>\n<ul class=\"\">\n<li id=\"4d7b\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm ok ol om on gc\" data-selectable-paragraph=\"\">What will happen if we override the\u00a0<code class=\"fr np nq nr ns b\">start()<\/code>\u00a0method in the Thread class?<br \/>\nYes, but lets say we are invoking\u00a0<code class=\"fr np nq nr ns b\">start()<\/code>\u00a0method in\u00a0<code class=\"fr np nq nr ns b\">Printer class<\/code>, what it does is, it will look for the immediate class and it has the method so it will run and it won\u2019t go to the super class to create a thread. So is there any way to create the thread even overriding the\u00a0<code class=\"fr np nq nr ns b\">start()<\/code>\u00a0method? answer is yes we can, by simply putting\u00a0<code class=\"fr np nq nr ns b\">super.start()<\/code>\u00a0inside the\u00a0<code class=\"fr np nq nr ns b\">start()<\/code>\u00a0method of the Printer class.<\/li>\n<li id=\"c371\" class=\"of og ji lu b lv oo lz op lf oq lj or ln os mm ok ol om on gc\" data-selectable-paragraph=\"\">What will happen if we overload the\u00a0<code class=\"fr np nq nr ns b\">run()<\/code>\u00a0method?<br \/>\nYes, but Thread classes\u2019 start method always invoke with the no arguments.<\/li>\n<li id=\"735f\" class=\"of og ji lu b lv oo lz op lf oq lj or ln os mm ok ol om on gc\" data-selectable-paragraph=\"\">Most of the programmers think that the java program will terminate once the main thread terminates. But in reality its not a daemon thread so the child thread can actually continue. what we can do is, we can change the printer thread object to a daemon thread by giving\u00a0<code class=\"fr np nq nr ns b\">printer.setDaemon(true);<\/code>\u00a0in the main class. At this point when you run the program, when the main thread ends the child thread also suppose to end but you may notice that the child thread will run for a certain period of time even after the main thread ends because at the moment when the main thread printing the last line, the child thread already processed up to certain values and the delay is occurred because of the time taken to print.<\/li>\n<li id=\"2cb3\" class=\"of og ji lu b lv oo lz op lf oq lj or ln os mm ok ol om on gc\" data-selectable-paragraph=\"\">The main disadvantage of this method is, we will loose the hierarchy of the classes when we extend Thread class to that particular class because Java does not support multiple inheritances.<\/li>\n<\/ul>\n<figure class=\"mo mp mq mr gz kj gn go paragraph-image\">\n<div class=\"gn go px\">\n<h4 style=\"text-align: center;\"><strong><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\">Grab the opportunity to learn Java with Entri! Click Here<\/a><\/strong><\/h4>\n<\/div>\n<\/figure>\n<h3 id=\"0320\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Implement Runnable Interface<\/strong><\/h3>\n<figure class=\"mo mp mq mr gz kj\">\n<div class=\"m l dq\">\n<div class=\"aek ou l\"><\/div>\n<\/div>\n<\/figure>\n<p id=\"341d\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">In the above code we have used implements Runnable instead of extending from Thread class.<\/p>\n<figure class=\"mo mp mq mr gz kj\">\n<div class=\"m l dq\">\n<div class=\"ael ou l\"><\/div>\n<\/div>\n<\/figure>\n<p id=\"1cae\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">Runnable interface is a\u00a0<strong class=\"lu jj\">SAM (Single Abstract Method) <\/strong>which\u00a0has a single method called run and thats it. In this case we don\u2019t have intermediate Thread class so we don\u2019t have someone to implement the <code class=\"fr np nq nr ns b\">run()<\/code>\u00a0method. So what we did here is, We create an instance from a Thread class (which means we can pass Runnable instance to that as I mentioned in the previous thread creating method and also to give thread behavior) called\u00a0<code class=\"fr np nq nr ns b\">thread<\/code>\u00a0and I pass the object\u00a0<code class=\"fr np nq nr ns b\">printer<\/code>\u00a0as the parameter to the thread instance. Now\u00a0<code class=\"fr np nq nr ns b\">printer<\/code>\u00a0is a\u00a0<strong class=\"lu jj\">Runnable class<\/strong>. That is why I invoked the start method from thread instance. And I got the output as follows,<\/p>\n<pre class=\"mo mp mq mr gz ov bt ow\"><span id=\"7eea\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Main thread is running....\r\nChild thread is running....\r\nmain main 0\r\nchild  0\r\nmain main 1\r\nchild  1\r\nmain main 2\r\nchild  2\r\nmain main 3\r\nchild  3\r\nchild  4\r\nchild  5\r\nmain main 4\r\nchild  6\r\nmain main 5\r\nchild  7\r\nmain main 6\r\nchild  8\r\nmain main 7\r\nchild  9\r\nmain main 8\r\nchild  10\r\nmain main 9\r\nchild  11\r\nMain thread Ends here\r\nchild  12\r\nchild  13\r\nchild  14\r\nchild  15\r\nchild  16\r\nchild  17\r\nchild  18\r\nchild  19\r\n===========================<\/span><\/pre>\n<p id=\"63eb\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">So this is the method of creating thread in the second approach.<\/p>\n<p id=\"989e\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">There are eight constructors in the Thread class such as,<\/p>\n<ul class=\"\">\n<li id=\"6d79\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm ok ol om on gc\" data-selectable-paragraph=\"\">Thread()<\/li>\n<\/ul>\n<pre class=\"mo mp mq mr gz ov bt ow\"><span id=\"c9bd\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Thread T1 = new Thread();<\/span><\/pre>\n<ul class=\"\">\n<li id=\"f335\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm ok ol om on gc\" data-selectable-paragraph=\"\">Thread(Runnable target)<\/li>\n<\/ul>\n<pre class=\"mo mp mq mr gz ov bt ow\"><span id=\"3567\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Thread T2 = new Thread(printer);<\/span><\/pre>\n<ul class=\"\">\n<li id=\"d731\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm ok ol om on gc\" data-selectable-paragraph=\"\">Thread(String name)<\/li>\n<\/ul>\n<pre class=\"mo mp mq mr gz ov bt ow\"><span id=\"03a2\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Thread T3 = new Thread(name:\"printerThread\");<\/span><\/pre>\n<ul class=\"\">\n<li id=\"b0cb\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm ok ol om on gc\" data-selectable-paragraph=\"\">Threat(Runnable target, String name)<\/li>\n<\/ul>\n<pre class=\"mo mp mq mr gz ov bt ow\"><span id=\"4184\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Thread T4 = new Thread(printer, name:\"printerThread\");<\/span><\/pre>\n<ul class=\"\">\n<li id=\"61c0\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm ok ol om on gc\" data-selectable-paragraph=\"\">Thread(ThreadGroup group, String name)<\/li>\n<\/ul>\n<pre class=\"mo mp mq mr gz ov bt ow\"><span id=\"d26c\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Thread T5 = new Thread(new ThreadGroup(),name:\"printerThread\");<\/span><\/pre>\n<ul class=\"\">\n<li id=\"8715\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm ok ol om on gc\" data-selectable-paragraph=\"\">Thread(ThreadGroup group, Runnable target)<\/li>\n<\/ul>\n<pre class=\"mo mp mq mr gz ov bt ow\"><span id=\"b8bf\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Thread T6 = new Thread(new ThreadGroup(),printer);<\/span><\/pre>\n<ul class=\"\">\n<li id=\"2154\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm ok ol om on gc\" data-selectable-paragraph=\"\">Thread(ThreadGroup group, Runnable target, String name)<\/li>\n<\/ul>\n<pre class=\"mo mp mq mr gz ov bt ow\"><span id=\"5afd\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Thread T7 = new Thread(new ThreadGroup(),printer,name:\"printerThread\");<\/span><\/pre>\n<ul class=\"\">\n<li id=\"0657\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm ok ol om on gc\" data-selectable-paragraph=\"\">Thread(ThreadGroup group, Runnable target, String name, long stack size)<\/li>\n<\/ul>\n<\/div>\n<div class=\"o dz nt nu ik nv\" role=\"separator\">\n<h4 id=\"2a00\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong style=\"font-size: 1.953em;\">Thread Priority<\/strong><\/h4>\n<\/div>\n<div class=\"jb jc jd je jf\">\n<p id=\"98b0\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">Every thread has a thread priority and it will run according to the priority. I to 10 is the Range of thread priority in Java. Basically 10 is the highest priority, 1 is the lowest and 5 is the normal priority.<\/p>\n<p id=\"dc79\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">So to set the priority we have to invoke setPriority method to the thread.<\/p>\n<h3 id=\"d91d\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Facts about the Thread Priority<\/strong><\/h3>\n<ul class=\"\">\n<li id=\"8293\" class=\"of og ji lu b lv lw lz ma lf pa lj pb ln pc mm ok ol om on gc\" data-selectable-paragraph=\"\">Most of the programmers think that 5 is the default priority for every threads but that is not the case. Lets say there are two threads T1(main) and T2(child) and the thread priority is set to 5 for both threads. In reality there will be no changes. There is actually a rule in Thread Priority which is Main thread\u2019s default priority value s 5 because it was created by the system. thereafter any thread that are created will inherit the parent thread priority value. So when creating the T1, the priority will be 5 and once we create T2 from T1, it will also take the same value even we didn\u2019t set anything. But later we can set the priority by invoking the\u00a0<code class=\"fr np nq nr ns b\">setPriority()<\/code>\u00a0method to the particular thread.<\/li>\n<li id=\"6c0a\" class=\"of og ji lu b lv oo lz op lf oq lj or ln os mm ok ol om on gc\" data-selectable-paragraph=\"\">What will happen when we give priority value which are not in the range like 11 then what will happen ?<br \/>\nI\u2019ve tried to run a program by giving the thread priority to 11 and I got the output as follows,<\/li>\n<\/ul>\n<pre class=\"mo mp mq mr gz ov bt ow\"><span id=\"3b01\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">Exception in thread \"main\" java.lang.IllegalArgumentException\r\n    at java.base\/java.lang.Thread.setPriority(Thread.java:1137)\r\n    at threadSample.Application.main(Application.java:9)<\/span><\/pre>\n<p id=\"1f5c\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">as you can see I got an error saying\u00a0<code class=\"fr np nq nr ns b\">IllegalArgumentException<\/code>. So there is no chance it will automatically set to 10.<\/p>\n<ul class=\"\">\n<li id=\"e95e\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm ok ol om on gc\" data-selectable-paragraph=\"\">What will happen when we give priority value 1 and 10 for threads T1 and T2 respectively?<br \/>\nMost of the time there won\u2019t be any big difference in the order of execution. Yes, the JVM will listen to the priority but we can\u2019t be so sure that JVM will do accordingly. So if you want to see how it works, we have to implement this in the real project and there is way we can take the <strong class=\"lu jj\">Thread dump\u00a0<\/strong>and see.<\/li>\n<\/ul>\n<h4 style=\"text-align: center;\"><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\"><strong>Learn to code from industry experts! Enroll here<\/strong><\/a><\/h4>\n<\/div>\n<div><\/div>\n<div class=\"jb jc jd je jf\"><strong style=\"font-size: 1.953em;\">Other Methods in Thread<\/strong><\/div>\n<div class=\"jb jc jd je jf\">\n<h3 id=\"e7e2\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Join Method<\/strong><\/h3>\n<p id=\"8493\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">Lets say there are two threads T1 and T2. T1 wants to wait for T2 to complete the task, then T1 should call the join method on T2 thread. We also can set time for T1 to wait. lets see what are those ways to call join method.<\/p>\n<p id=\"40e3\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\"><code class=\"fr np nq nr ns b\">T2.join()<\/code>\u00a0\u2014 this will wait forever or until T2 dies<\/p>\n<p id=\"a84b\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\"><code class=\"fr np nq nr ns b\">T2.join(long millis)<\/code>\u00a0\u2014this will wait\u00a0<code class=\"fr np nq nr ns b\">millis<\/code>\u00a0milliseconds for this thread to die.<\/p>\n<p id=\"e372\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\"><code class=\"fr np nq nr ns b\">T2.join(long millis,int nanos)<\/code>\u00a0\u2014 this will wait\u00a0<code class=\"fr np nq nr ns b\">millis<\/code>\u00a0milliseconds plus\u00a0<code class=\"fr np nq nr ns b\">nanos<\/code>\u00a0nanoseconds for this thread to die.<\/p>\n<p id=\"3847\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">So whenever the join method is called, the thread will go to waiting state from Running state. In this example T1 thread will go to waiting state. Lets see on what case the T1 will go back to the running state,<\/p>\n<ol class=\"\">\n<li id=\"2062\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm py ol om on gc\" data-selectable-paragraph=\"\">T2 complete its process<\/li>\n<li id=\"5dcb\" class=\"of og ji lu b lv oo lz op lf oq lj or ln os mm py ol om on gc\" data-selectable-paragraph=\"\">Timeout (only if the time is set)<\/li>\n<li id=\"5ce6\" class=\"of og ji lu b lv oo lz op lf oq lj or ln os mm py ol om on gc\" data-selectable-paragraph=\"\">When it is interrupted<\/li>\n<\/ol>\n<h3 id=\"62d4\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Yield Method<\/strong><\/h3>\n<p id=\"65e3\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">When the\u00a0<code class=\"fr np nq nr ns b\">yield()<\/code>\u00a0method is invoked, then it will send a hint to the scheduler that the current thread is willing to yield its current use of a processor. It is a native method because it not implemented in Java. Lets say there are three threads T1,T2 and T3. So once the T1 calls the\u00a0<code class=\"fr np nq nr ns b\">yield()<\/code> method, the scheduler will give chance to other threads and it is not sure that T2 or T3 will get the chance immediately. Lets say T2 is getting the chance and once it completes at that moment also we can\u2019t say that the scheduler will give the chance to T1. Its totally depend on the process called <strong class=\"lu jj\">Primitive Scheduling\u00a0<\/strong>and if the platform doesn\u2019t support this process, you won\u2019t able to see these kind of execution.<\/p>\n<h4 style=\"text-align: center;\"><strong><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\">Grab the opportunity to learn Java with Entri! Click Here<\/a><\/strong><\/h4>\n<h3 id=\"50fb\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Sleep Method<\/strong><\/h3>\n<p id=\"6f3c\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">When this method is called, it can wait for a certain amount of time. Here, there are two different approach to call this method such as,<\/p>\n<ol class=\"\">\n<li id=\"9f19\" class=\"of og ji lu b lv ms lz mt lf oh lj oi ln oj mm py ol om on gc\" data-selectable-paragraph=\"\"><code class=\"fr np nq nr ns b\">sleep(long millis)<\/code>\u00a0method which is a native method and simply we can give the sleep time in the parameter.<\/li>\n<li id=\"515a\" class=\"of og ji lu b lv oo lz op lf oq lj or ln os mm py ol om on gc\" data-selectable-paragraph=\"\"><code class=\"fr np nq nr ns b\">sleep(long millis,int nanos)<\/code>\u00a0method which is not a native method(implemented in Java).<\/li>\n<\/ol>\n<p id=\"e7db\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">So if the sleep time finishes or if there is any interruption then the thread will go back to the running state.<\/p>\n<h3 id=\"4382\" class=\"ku kv ji bn kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr gc\"><strong>Interrupt Method<\/strong><\/h3>\n<p id=\"431b\" class=\"pw-post-body-paragraph ls lt ji lu b lv lw lx ly lz ma mb mc lf md me mf lj mg mh mi ln mj mk ml mm jb gc\" data-selectable-paragraph=\"\">When this method is invoked, the particular thread will comeback to the ready state from the waiting state. That is why when calling a sleep method we should include try catch method as follows,<\/p>\n<pre class=\"mo mp mq mr gz ov bt ow\"><span id=\"f78d\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">try {\r\n     Thread.sleep(1000);\r\n} catch (InterruptedException e) {\r\n    \/\/ TODO Auto-generated catch block\r\n    e.printStackTrace();\r\n}<\/span><\/pre>\n<p id=\"7e1e\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">so to interrupt the thread we have to call the method as follows,<\/p>\n<pre class=\"mo mp mq mr gz ov bt ow\"><span id=\"7e07\" class=\"gc ku kv ji ns b do ox oy l oz\" data-selectable-paragraph=\"\">thread.interrupt();<\/span><\/pre>\n<h3><a href=\"https:\/\/bit.ly\/3ELmCiA\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-25494072 size-full\" src=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square.png\" alt=\"\" width=\"345\" height=\"345\" srcset=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square.png 345w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square-300x300.png 300w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square-150x150.png 150w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square-24x24.png 24w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square-48x48.png 48w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square-96x96.png 96w, https:\/\/entri.app\/blog\/wp-content\/uploads\/2021\/10\/Web-Development-Square-75x75.png 75w\" sizes=\"auto, (max-width: 345px) 100vw, 345px\" \/><\/a><\/h3>\n<p id=\"2fde\" class=\"pw-post-body-paragraph ls lt ji lu b lv ms lx ly lz mt mb mc lf mu me mf lj mv mh mi ln mw mk ml mm jb gc\" data-selectable-paragraph=\"\">In our example lets say<code class=\"fr np nq nr ns b\">thread<\/code>\u00a0is sleeping for 5000 ms and once we call the above method it will come back to the ready state. Important note here is one interrupt will only work for one sleep method. What if we call this method on the\u00a0<code class=\"fr np nq nr ns b\">thread<\/code>\u00a0which is not sleeping. This will wait and execute the\u00a0<code class=\"fr np nq nr ns b\">interrupt()<\/code>\u00a0method once the\u00a0<code class=\"fr np nq nr ns b\">sleep()<\/code>\u00a0method is invoked until that it won\u2019t do anything.<\/p>\n<\/div>\n<div class=\"o dz nt nu ik nv\" role=\"separator\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Define thread in Java Thread is a\u00a0flow of execution\u00a0through the process code and the process can contain multiple threads which is known as\u00a0Multi-threading. Basically its a multiple flow of execution. So running multiple task at a certain time. This is where most of the people misunderstood, let\u2019s say there is a process which runs for [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":25523497,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[802,1882],"tags":[],"class_list":["post-25523397","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","category-java-programming"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is a thread in Java- All About Java Threads - Entri Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is a thread in Java- All About Java Threads - Entri Blog\" \/>\n<meta property=\"og:description\" content=\"Define thread in Java Thread is a\u00a0flow of execution\u00a0through the process code and the process can contain multiple threads which is known as\u00a0Multi-threading. Basically its a multiple flow of execution. So running multiple task at a certain time. This is where most of the people misunderstood, let\u2019s say there is a process which runs for [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/\" \/>\n<meta property=\"og:site_name\" content=\"Entri Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/entri.me\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-05-07T23:37:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-23T14:51:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/05\/Understanding-Java-threads-All-About-Java-Threads.png\" \/>\n\t<meta property=\"og:image:width\" content=\"820\" \/>\n\t<meta property=\"og:image:height\" content=\"615\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Feeba Mahin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@entri_app\" \/>\n<meta name=\"twitter:site\" content=\"@entri_app\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Feeba Mahin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/\"},\"author\":{\"name\":\"Feeba Mahin\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36\"},\"headline\":\"What is a thread in Java- All About Java Threads\",\"datePublished\":\"2022-05-07T23:37:19+00:00\",\"dateModified\":\"2022-11-23T14:51:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/\"},\"wordCount\":2287,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/05\/Understanding-Java-threads-All-About-Java-Threads.png\",\"articleSection\":[\"Articles\",\"Java Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/\",\"url\":\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/\",\"name\":\"What is a thread in Java- All About Java Threads - Entri Blog\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/05\/Understanding-Java-threads-All-About-Java-Threads.png\",\"datePublished\":\"2022-05-07T23:37:19+00:00\",\"dateModified\":\"2022-11-23T14:51:59+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#primaryimage\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/05\/Understanding-Java-threads-All-About-Java-Threads.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/05\/Understanding-Java-threads-All-About-Java-Threads.png\",\"width\":820,\"height\":615,\"caption\":\"Understanding Java threads, All About Java Threads\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/entri.app\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Entri Skilling\",\"item\":\"https:\/\/entri.app\/blog\/category\/entri-skilling\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Java Programming\",\"item\":\"https:\/\/entri.app\/blog\/category\/entri-skilling\/java-programming\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"What is a thread in Java- All About Java Threads\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/entri.app\/blog\/#website\",\"url\":\"https:\/\/entri.app\/blog\/\",\"name\":\"Entri Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/entri.app\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/entri.app\/blog\/#organization\",\"name\":\"Entri App\",\"url\":\"https:\/\/entri.app\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png\",\"width\":989,\"height\":446,\"caption\":\"Entri App\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/entri.me\/\",\"https:\/\/x.com\/entri_app\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36\",\"name\":\"Feeba Mahin\",\"url\":\"https:\/\/entri.app\/blog\/author\/feeba123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is a thread in Java- All About Java Threads - Entri Blog","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:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/","og_locale":"en_US","og_type":"article","og_title":"What is a thread in Java- All About Java Threads - Entri Blog","og_description":"Define thread in Java Thread is a\u00a0flow of execution\u00a0through the process code and the process can contain multiple threads which is known as\u00a0Multi-threading. Basically its a multiple flow of execution. So running multiple task at a certain time. This is where most of the people misunderstood, let\u2019s say there is a process which runs for [&hellip;]","og_url":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/","og_site_name":"Entri Blog","article_publisher":"https:\/\/www.facebook.com\/entri.me\/","article_published_time":"2022-05-07T23:37:19+00:00","article_modified_time":"2022-11-23T14:51:59+00:00","og_image":[{"width":820,"height":615,"url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/05\/Understanding-Java-threads-All-About-Java-Threads.png","type":"image\/png"}],"author":"Feeba Mahin","twitter_card":"summary_large_image","twitter_creator":"@entri_app","twitter_site":"@entri_app","twitter_misc":{"Written by":"Feeba Mahin","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#article","isPartOf":{"@id":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/"},"author":{"name":"Feeba Mahin","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36"},"headline":"What is a thread in Java- All About Java Threads","datePublished":"2022-05-07T23:37:19+00:00","dateModified":"2022-11-23T14:51:59+00:00","mainEntityOfPage":{"@id":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/"},"wordCount":2287,"commentCount":0,"publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"image":{"@id":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/05\/Understanding-Java-threads-All-About-Java-Threads.png","articleSection":["Articles","Java Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/","url":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/","name":"What is a thread in Java- All About Java Threads - Entri Blog","isPartOf":{"@id":"https:\/\/entri.app\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#primaryimage"},"image":{"@id":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/05\/Understanding-Java-threads-All-About-Java-Threads.png","datePublished":"2022-05-07T23:37:19+00:00","dateModified":"2022-11-23T14:51:59+00:00","breadcrumb":{"@id":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#primaryimage","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/05\/Understanding-Java-threads-All-About-Java-Threads.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/05\/Understanding-Java-threads-All-About-Java-Threads.png","width":820,"height":615,"caption":"Understanding Java threads, All About Java Threads"},{"@type":"BreadcrumbList","@id":"https:\/\/entri.app\/blog\/understanding-java-threads-all-about-java-threads\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/entri.app\/blog\/"},{"@type":"ListItem","position":2,"name":"Entri Skilling","item":"https:\/\/entri.app\/blog\/category\/entri-skilling\/"},{"@type":"ListItem","position":3,"name":"Java Programming","item":"https:\/\/entri.app\/blog\/category\/entri-skilling\/java-programming\/"},{"@type":"ListItem","position":4,"name":"What is a thread in Java- All About Java Threads"}]},{"@type":"WebSite","@id":"https:\/\/entri.app\/blog\/#website","url":"https:\/\/entri.app\/blog\/","name":"Entri Blog","description":"","publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/entri.app\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/entri.app\/blog\/#organization","name":"Entri App","url":"https:\/\/entri.app\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png","width":989,"height":446,"caption":"Entri App"},"image":{"@id":"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/entri.me\/","https:\/\/x.com\/entri_app"]},{"@type":"Person","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/f036dab84abae3dcc9390a1110d95d36","name":"Feeba Mahin","url":"https:\/\/entri.app\/blog\/author\/feeba123\/"}]}},"_links":{"self":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25523397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/comments?post=25523397"}],"version-history":[{"count":10,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25523397\/revisions"}],"predecessor-version":[{"id":25548148,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25523397\/revisions\/25548148"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media\/25523497"}],"wp:attachment":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media?parent=25523397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/categories?post=25523397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/tags?post=25523397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}