<div dir="ltr" style="text-align: justify;" trbidi="on">In this part of tutorial we will learn how to use the <i><b>join </b></i>method in the <b><i>Thread</i></b>. Then We will create an example of Thread with the use of join method. </p>
<ul>
<li>Java Join method join the next thread at the end of the current thread</li>
<li>After current thread stops execution then next thread executes.</li>
</ul>
<p><b>Syntax:</b></p>
<pre class="highlight" name="code">public void join() throws InterruptedException 
public void join(long miliseconds) throws InterruptedException 
</pre>
<p></p>
<pre class="highlight" name="code">class JoinDemo extends Thread{ 
 public void run(){ 
 for(int i=1;i<;=5;i++){ 
 try{ 
 Thread.sleep(500); 
 }catch(Exception e){ 
 System.out.println(e); 
 } 
 System.out.println("Dinesh on Java "+i); 
 } 
 } 
public static void main(String args[]){ 
 JoinDemo t1 = new JoinDemo(); 
 JoinDemo t2 = new JoinDemo(); 
 JoinDemo t3 = new JoinDemo(); 
 t1.start(); 
 try{ 
 t1.join(); 
 }catch(Exception e){ 
 System.out.println(e); 
 } 
 
 t2.start(); 
 t3.start(); 
 } 
} 
</pre>
<div align='center' id="ads-id"></div>
<p>As you can see in the above example,when t1 completes its task then t2 and t3 starts executing.<br />
<b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/join.png"/></div>
<p></p>
<pre class="highlight" name="code">//Example of join(long miliseconds) method 
class JoinDemo extends Thread{ 
 public void run(){ 
 for(int i=1;i<;=5;i++){ 
 try{ 
 Thread.sleep(500); 
 }catch(Exception e){ 
 System.out.println(e); 
 } 
 System.out.println("Dinesh on Java "+i); 
 } 
 } 
public static void main(String args[]){ 
 JoinDemo t1 = new JoinDemo(); 
 JoinDemo t2 = new JoinDemo(); 
 JoinDemo t3 = new JoinDemo(); 
 t1.start(); 
 try{ 
 t1.join(1500); 
 }catch(Exception e){ 
 System.out.println(e); 
 } 
 
 t2.start(); 
 t3.start(); 
 } 
} 
</pre>
<p>In the above example,when t1 is completes its task for 1500 <b>milliseconds</b>(3 times) then t2 and t3 starts executing.<br />
<b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><b><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/join2.png"/></b></div>
<div id="Naming_a_thread"><b>Naming a thread:</b><br />
The Thread class provides methods to change and get the name of a thread. </div>
<p><b>Using <i>getName()</i>,<i> setName(String)</i> and <i>getId()</i> method:</b></p>
<pre class="highlight" name="code">public String getName()//is used to return the name of a thread. 
public void setName(String name)//is used to change the name of a thread. 
public long getId()//is used to return the id of a thread. 
</pre>
<p></p>
<pre class="highlight" name="code">class JoinDemo extends Thread{ 
 public void run(){ 
 for(int i=1;i<;=5;i++){ 
 try{ 
 Thread.sleep(500); 
 }catch(Exception e){ 
 System.out.println(e); 
 } 
 System.out.println("Running thread is "+Thread.currentThread().getName()+" : "+i); 
 } 
 } 
public static void main(String args[]){ 
 JoinDemo t1 = new JoinDemo(); 
 JoinDemo t2 = new JoinDemo(); 
 JoinDemo t3 = new JoinDemo(); 
 t1.setName("Dinesh on Java"); 
 System.out.println("id of t1:"+t1.getId()); 
 System.out.println("id of t2:"+t2.getId()); 
 System.out.println("id of t3:"+t3.getId()); 
 t1.start(); 
 try{ 
 t1.join(1500); 
 }catch(Exception e){ 
 System.out.println(e); 
 } 
 
 t2.start(); 
 t3.start(); 
 } 
} 
</pre>
<p><b>The <i>currentThread()</i> method:</b><br />
The <i><b>currentThread()</b></i> method returns a reference to the currently executing thread object.<br />
<b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/join3.png"/></div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/sleeping-thread-using-sleep-method/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/priority-of-thread/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/sleeping-thread-using-sleep-method/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/priority-of-thread/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '480'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…