{"id":1875,"date":"2018-12-19T12:00:00","date_gmt":"2018-12-19T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/threading-and-multithreading-in-java\/"},"modified":"2026-07-05T03:21:39","modified_gmt":"2026-07-05T01:21:39","slug":"threading-and-multithreading-in-java","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/threading-and-multithreading-in-java\/","title":{"rendered":"Threading and Multithreading in Java"},"content":{"rendered":"<p><strong>Other Java Topics<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/interfaces-in-java-basics-of-java-interface\/\">Interfaces in Java<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/threading-and-multithreading-in-java\/\">Threading and Multithreading in Java<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/generics-in-java-generic-classes-and-methods-in-java\/\">Introduction to Generics in Java<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/more-on-java-generics-bounded-generics\/\">Bounded Generics in Java<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/generic-classes-in-java\/\">Generic Classes in Java<\/a><\/li>\n<\/ul>\n<p><strong>Multithreading<\/strong> is supported in Java. So we can write multithreaded program or applications using Java.\u00a0 A multi-threaded program is made up of two or more component part that can run concurrently independent of each other. These parts can perform different tasks seperatelly without interupting other programs.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-240 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Threading-in-Java-300x170.jpg\" alt=\"\" width=\"300\" height=\"170\" \/><\/p>\n<p><strong>Life Cycle of a Java Thread<\/strong><\/p>\n<p>A thread in java goes through the following life cycle:<\/p>\n<p><strong>New:<\/strong> This is the state of the thread when it is first created. At this state, no program is using it. A thread enters the New state as soon as an instance of the thread is created<\/p>\n<p><strong>Runnable:<\/strong> A thread enters the runnable start as soon as the start() method of the thread is called. This is equivalent to the ready state. It is ready to run but the scheduler has not selected it to run<\/p>\n<p><strong>Running:<\/strong> In the running state, the thread has been selected to run and is actually executing a task.<\/p>\n<p><strong>Waiting(Blocked):<\/strong> In this state, the thread cannot run due to some related factor. For example, it&#8217;s waiting for a resource used by another program to be released.<\/p>\n<p><strong>Terminated(Dead):<\/strong> After completing the task, the tread ends its execution and exits. This is the dead or terminated state.<\/p>\n<p>This is shown in Figure 1.0.<\/p>\n<figure id=\"attachment_238\" aria-describedby=\"caption-attachment-238\" style=\"width: 735px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-238 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Thread-Life-Cycle-in-Java-1024x411.jpg\" alt=\"\" width=\"735\" height=\"295\" \/><figcaption id=\"caption-attachment-238\" class=\"wp-caption-text\">Figure 1.0: Thread Life Cycle in Java<\/figcaption><\/figure>\n<p><strong>Properties of a Thread<\/strong><\/p>\n<p>One important property of a thread is the priority. The priority of a thread determines the order of schdulling for the thread by the operating system. This is such that threads with higher priorities are scheduled first.<\/p>\n<p>Thread priorities in java have a range from MIN_PRIORITY (1)\u00a0 and MAX_PRIORITY(0). There is also a NORM_PRIORITY (5) which every thread receives by default<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How\u00a0 to Create a Thread in Java<\/strong><\/p>\n<p>There are two methods of creating a thread in Java:<\/p>\n<p>a.\u00a0 By Extending the Thread Class<\/p>\n<p>b. By Implementing the Runnable Interface<\/p>\n<p>&nbsp;<\/p>\n<p><strong>a. Extending the Thread Class<\/strong><\/p>\n<p>In this method, you need to create a class that extends the Thread class which is available in the java.lang.* package. Next, you need to override the run() method.\u00a0 Then you need to create a thread object and call its start method.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-style: italic;\">\/\/Class MyThread<\/span>\n\n<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">class<\/span> MyThread <span style=\"color: #000080; font-weight: bold;\">extends<\/span> Thread {\n\t<span style=\"color: #000080; font-weight: bold;\">private<\/span> Thread t;\n\t<span style=\"color: #000080; font-weight: bold;\">private<\/span> String ThreadName;\n\t<span style=\"color: #008800; font-style: italic;\">\/\/Define a constructor<\/span>\n\t<span style=\"color: #000080; font-weight: bold;\">public<\/span> MyThread(String name)\n\t{\n\t\tSystem.<span style=\"color: #ff0000;\">out<\/span>.<span style=\"color: #ff0000;\">println<\/span>(<span style=\"color: #0000ff;\">\"Thread: \"<\/span> + name + <span style=\"color: #0000ff;\">\" Created\"<\/span>);\n\t}\n\t\n\t<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">void<\/span> run()\n\t{  \n\t\t<span style=\"color: #000080; font-weight: bold;\">try<\/span> {\n\t\t\tSystem.<span style=\"color: #ff0000;\">out<\/span>.<span style=\"color: #ff0000;\">println<\/span>(<span style=\"color: #0000ff;\">\"The thread running now is: \"<\/span> + ThreadName);\n\t\t} <span style=\"color: #000080; font-weight: bold;\">catch<\/span> (Exception e) {\n\t\t\tSystem.<span style=\"color: #ff0000;\">out<\/span>.<span style=\"color: #ff0000;\">println<\/span>(<span style=\"color: #0000ff;\">\"Thread: \"<\/span> + ThreadName + <span style=\"color: #0000ff;\">\" interrupted\"<\/span>);\n\t\t}\n\t}\n\t\n\t<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">void<\/span> start()\n\t{\n\t\tSystem.<span style=\"color: #ff0000;\">out<\/span>.<span style=\"color: #ff0000;\">println<\/span>(<span style=\"color: #0000ff;\">\"Started Thread: \"<\/span> + ThreadName);\n\t\t<span style=\"color: #000080; font-weight: bold;\">if<\/span>(t == <span style=\"color: #000080; font-weight: bold;\">null<\/span>)\n\t\t{\n\t\t\tt = <span style=\"color: #000080; font-weight: bold;\">new<\/span> Thread (<span style=\"color: #000080; font-weight: bold;\">this<\/span>, <span style=\"color: #0000ff;\">\"t1\"<\/span>);\n\t\t\tt.<span style=\"color: #ff0000;\">start<\/span>();\n\t\t}\n\t}\n}\n<\/pre>\n<\/div>\n<p>Listing 1.0: Thread Class that extends Thread<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #000080; font-weight: bold;\">import<\/span> myitems.MyThread;\n<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">class<\/span> RunProgs {\n\n\t<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">static<\/span> <span style=\"color: #000080; font-weight: bold;\">void<\/span> main(String[] args) {\n\t\t<span style=\"color: #008800; font-style: italic;\">\/\/ TODO Auto-generated method stub<\/span>\n\t\tMyThread t1 = <span style=\"color: #000080; font-weight: bold;\">new<\/span> MyThread(<span style=\"color: #0000ff;\">\"t1\"<\/span>);\n\t\tt1.<span style=\"color: #ff0000;\">start<\/span>();\n\t\t\n\t\tMyThread t2 = <span style=\"color: #000080; font-weight: bold;\">new<\/span> MyThread(<span style=\"color: #0000ff;\">\"t2\"<\/span>);\n\t\t<span style=\"color: #000080; font-weight: bold;\">try<\/span> {\n\t\t\tt2.<span style=\"color: #ff0000;\">sleep<\/span>(<span style=\"color: #0000ff;\">1000<\/span>);\n\t\t} <span style=\"color: #000080; font-weight: bold;\">catch<\/span> (InterruptedException e) {\n\t\t\t<span style=\"color: #008800; font-style: italic;\">\/\/ TODO Auto-generated catch block<\/span>\n\t\t\te.<span style=\"color: #ff0000;\">printStackTrace<\/span>();\n\t\t}\n\t\tt2.<span style=\"color: #ff0000;\">start<\/span>();\n\t\t\n\t\tMyThread t3 = <span style=\"color: #000080; font-weight: bold;\">new<\/span> MyThread(<span style=\"color: #0000ff;\">\"t3\"<\/span>);\n\t\tt3.<span style=\"color: #ff0000;\">start<\/span>();\n\t}\t\t\t\n}\n<\/pre>\n<\/div>\n<p>Listing 1.1: Testing Threading Implementation<\/p>\n<p><strong>b. Implementing the Runnable Interface<\/strong><\/p>\n<p>This is the second way to create a thread in Java. In this method, you create a class that\u00a0 implements the runnable interface.\u00a0 Then you must provide an implementation for the run method and the start methods defined in the runnable interface.<\/p>\n<p>This is shown in Listing 1.2.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">class<\/span> RunnableThread <span style=\"color: #000080; font-weight: bold;\">implements<\/span> Runnable {\n\n\t<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">void<\/span> run() {\n\t\t<span style=\"color: #000080; font-weight: bold;\">try<\/span> {\n\t\t\tSystem.<span style=\"color: #ff0000;\">out<\/span>.<span style=\"color: #ff0000;\">println<\/span>(<span style=\"color: #0000ff;\">\"The thread running is: \"<\/span> + Thread.<span style=\"color: #ff0000;\">currentThread<\/span>().<span style=\"color: #ff0000;\">getId<\/span>());\n\t\t} <span style=\"color: #000080; font-weight: bold;\">catch<\/span> (Exception e) {\n\t\t\tSystem.<span style=\"color: #ff0000;\">out<\/span>.<span style=\"color: #ff0000;\">println<\/span>(<span style=\"color: #0000ff;\">\"Error Occured: \"<\/span> + e.<span style=\"color: #ff0000;\">getMessage<\/span>());\n\t\t}\n\t}\n}\n<\/pre>\n<\/div>\n<p>Listing 1.1: Class RunnableThread implements Runnable<\/p>\n<p>Let&#8217;s now test this class by creating an object of RunnableThread type and calling the run method<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #000080; font-weight: bold;\">import<\/span> myitems.RunnableThread;\n<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">class<\/span> RunProgs {\n\n\t<span style=\"color: #000080; font-weight: bold;\">public<\/span> <span style=\"color: #000080; font-weight: bold;\">static<\/span> <span style=\"color: #000080; font-weight: bold;\">void<\/span> main(String[] args) {\n\t\tThread t1 = <span style=\"color: #000080; font-weight: bold;\">new<\/span> Thread(<span style=\"color: #000080; font-weight: bold;\">new<\/span> RunnableThread());\n\t\tt1.<span style=\"color: #ff0000;\">start<\/span>();\n\t\t\n\t\tThread t2 = <span style=\"color: #000080; font-weight: bold;\">new<\/span> Thread(<span style=\"color: #000080; font-weight: bold;\">new<\/span> RunnableThread());\n\t\tt2.<span style=\"color: #ff0000;\">start<\/span>();\n\t\t\n\t\tThread t3 = <span style=\"color: #000080; font-weight: bold;\">new<\/span> Thread(<span style=\"color: #000080; font-weight: bold;\">new<\/span> RunnableThread());\n\t\tt3.<span style=\"color: #ff0000;\">start<\/span>();\n\t}\t\t\t\n}\n<\/pre>\n<\/div>\n<p>Listing 1.2: Testing the RunnableThread class<\/p>\n<p>If you run this program, you will have an output as shown in the figure below.<\/p>\n<figure id=\"attachment_239\" aria-describedby=\"caption-attachment-239\" style=\"width: 938px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-239 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Program-Output-for-Threading.jpg\" alt=\"\" width=\"938\" height=\"609\" \/><figcaption id=\"caption-attachment-239\" class=\"wp-caption-text\">Figure 1.1: Program output for RunnableThread written in Eclipse IDE<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Other Java Topics Interfaces in Java Threading and Multithreading in Java Introduction to Generics in Java Bounded Generics in Java Generic Classes in Java Multithreading &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[85],"tags":[],"class_list":["post-1875","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1875"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1875\/revisions"}],"predecessor-version":[{"id":2043,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1875\/revisions\/2043"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}