{"id":344,"date":"2015-06-16T04:34:00","date_gmt":"2015-06-16T04:34:00","guid":{"rendered":"http:\/\/www.java2blog.com\/?p=344"},"modified":"2021-01-11T12:12:20","modified_gmt":"2021-01-11T06:42:20","slug":"java-thread-join-example","status":"publish","type":"post","link":"https:\/\/java2blog.com\/java-thread-join-example\/","title":{"rendered":"Java Thread Join Example"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">In this post, we will see about Java thread join method.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Variants_of_join_methods\">Variants of join methods<\/a><ul><li><a href=\"#public_final_void_join_throws_InterruptedException\">public final void join() throws InterruptedException<\/a><\/li><li><a href=\"#public_final_void_joinlong_millis_throws_InterruptedException\">public final void join(long millis) throws InterruptedException<\/a><\/li><li><a href=\"#public_final_void_joinlong_millisint_nanos_throws_InterruptedException\">public final void join(long millis,int nanos) throws InterruptedException<\/a><\/li><\/ul><\/li><li><a href=\"#Java_Thread_Join_example\">Java Thread Join example<\/a><\/li><li><a href=\"#Java_Thread_Join_and_Synchornization\">Java Thread Join and Synchornization<\/a><\/li><\/ul><\/div>\n\nThread class&#8217;s <code>join()<\/code> method can be used to stop current execution of thread until thread it joins, completes its task. So basically, it waits for the thread on which join method is getting called, to die.<strong>For example:<\/strong><br \/>\nHere when you call <code>t1.join()<\/code>, main thread will wait for t1 to complete its start before resuming its execution.<\/p>\n<pre class=\"lang:java decode:1 \" >\/\/ Main thread execution\nThread t1=new Thread(mr,\"Thread 1\");t1.start();\n\/\/ lets waits for t1 to die\ntry {\nt1.join();\n} catch (InterruptedException e) {\n<\/pre>\n<h2><span id=\"Variants_of_join_methods\">Variants of join methods<\/span><\/h2>\n<p>There are three variant of join method<\/p>\n<h3><span id=\"public_final_void_join_throws_InterruptedException\">public final void join() throws InterruptedException<\/span><\/h3>\n<div>\n<p>Thread on which join method is getting called, to die.<\/p>\n<h3><span id=\"public_final_void_joinlong_millis_throws_InterruptedException\">public final void join(long millis) throws InterruptedException<\/span><\/h3>\n<p>This method when called on the thread, it waits for either of following:<\/p>\n<\/div>\n<div>\n<ul style=\"text-align: left;\">\n<li>Thread on which join method is getting called, to die.<\/li>\n<li>Specified milliseconds<\/li>\n<\/ul>\n<div>\n<div>\n<h3><span id=\"public_final_void_joinlong_millisint_nanos_throws_InterruptedException\">public final void join(long millis,int nanos) throws InterruptedException<\/span><\/h3>\n<p>This method when called on the thread, it waits for either of following:<\/p>\n<\/div>\n<div>\n<ul>\n<li>Thread on which join method is getting called, to die.<\/li>\n<li>Specified milliseconds + nano seconds<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div>\n<blockquote><p><span style=\"font-size: 1.2em;\">&#x1f4a1; <strong>Did you know?<\/strong><\/span><\/p>\n<p>If the thread, on which join method is called, has been already terminated or has not been started yet, then join method returns immediately.<\/p><\/blockquote>\n<h2 style=\"text-align: left;\"><span id=\"Java_Thread_Join_example\">Java Thread Join example<\/span><\/h2>\n<p>Let&#8217;s take a simple example:<\/p>\n<pre name=\"code\" class=\"\" title=\"MyRunnable.java\">package org.arpit.java2blog;\n\npublic class MyRunnable implements Runnable {\n\n    public void run() {\n        try {\n            System.out.println(Thread.currentThread().getName() + \" Start\");\n            \/\/ thread sleeps for 4 secs\n            Thread.sleep(4000);\n            System.out.println(Thread.currentThread().getName() + \" end\");\n        } catch (InterruptedException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        }\n    }\n}<\/pre>\n<p>Create <code>ThreadExampleMain.java<\/code><\/p>\n<pre name=\"code\" class=\"\" title=\"ThreadExampleMain.java\">package org.arpit.java2blog;\n\npublic class ThreadExampleMain {\n\n    public static void main(String args[]) {\n\n        System.out.println(\"Main thread execution starts\");\n        MyRunnable mr = new MyRunnable();\n\n        Thread t1 = new Thread(mr, \"Thread 1\");\n        Thread t2 = new Thread(mr, \"Thread 2\");\n        Thread t3 = new Thread(mr, \"Thread 3\");\n\n        t1.start();\n        \/\/ lets waits for t1 to die\n        try {\n            t1.join();\n        } catch (InterruptedException e) {\n\n            e.printStackTrace();\n        }\n\n        t2.start();\n        try {\n            \/\/ lets waits for 1 sec or t2 to die which ever occurs first\n            t2.join(1000);\n\n        } catch (InterruptedException e1) {\n\n            e1.printStackTrace();\n        }\n        t3.start();\n\n        \/\/ complete all threads before completing main thread\n        try {\n            t2.join();\n            t3.join();\n\n        } catch (InterruptedException e1) {\n\n            e1.printStackTrace();\n        }\n        System.out.println(\"Main thread execution ends\");\n    }\n}\n\n<\/pre>\n<div>\n<p>When you run above program, you will get following output.<\/p>\n<div class=\"content-box-green\">Main thread execution starts<br \/>\nThread 1 Start<br \/>\nThread 1 end<br \/>\nThread 2 Start<br \/>\nThread 3 Start<br \/>\nThread 2 end<br \/>\nThread 3 end<br \/>\nMain thread execution ends<\/div>\n<p>Lets analysis output now.<\/p>\n<ol style=\"text-align: left;\">\n<li>Main thread execution starts.<\/li>\n<li><code>Thread 1<\/code> starts(Thread 1 start) and as we have put <code>t1.join()<\/code>, it will wait for <code>t1<\/code> to die(Thread 1 end).<\/li>\n<li><code>Thread 2<\/code> starts(Thread 2 start) and waits for either 1 seconds or die, but as we have put sleep for 4 seconds in run method, it will not die in 1 second. so main thread resumes and <code>Thread 3 <\/code>starts(Thread 3 start)<\/li>\n<li>As we have put <code>t2.join()<\/code> and <code>t3.join()<\/code>. These 2 threads will get completed before exiting main thread, so Thread 2 will end(Thread 2 end ) and then thread 3 will end(Thread 3 end).<\/li>\n<li>Main thread execution ends.<\/li>\n<\/ol>\n<div><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2><span id=\"Java_Thread_Join_and_Synchornization\">Java Thread Join and Synchornization<\/span><\/h2>\n<p>Java thread join method guarantees <a href=\"https:\/\/docs.oracle.com\/javase\/specs\/jls\/se8\/html\/jls-17.html#jls-17.4.5\" target=\"_blank\" rel=\"noopener noreferrer\">happens before<\/a> relationship.<\/p>\n<p>It means if thread <code>t1<\/code> calls <code>t2.join()<\/code>, then all the changes done by t2 are visible to t1.<\/p>\n<p>That&#8217;s all about Java Thread Join Example.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsVariants of join methodspublic final void join() throws InterruptedExceptionpublic final void join(long millis) throws InterruptedExceptionpublic final void join(long millis,int nanos) throws InterruptedExceptionJava Thread Join exampleJava Thread Join and Synchornization In this post, we will see about Java thread join method. Thread class&#8217;s join() method can be used to stop current execution of thread [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[9,144],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/344"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=344"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/344\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}