Java Multi threading

Java Thread Join using join() method

In this part of tutorial we will learn how to use the join method in the Thread. Then We will create an example of Thread with the use of join method.
  • Java Join method join the next thread at the end of the current thread
  • After current thread stops execution then next thread executes.

Syntax:

public void join() throws InterruptedException
public void join(long miliseconds) throws InterruptedException

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();
 }
}

As you can see in the above example,when t1 completes its task then t2 and t3 starts executing.
output:

//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();
 }
}

In the above example,when t1 is completes its task for 1500 milliseconds(3 times) then t2 and t3 starts executing.
output:

Naming a thread:
The Thread class provides methods to change and get the name of a thread.

Using getName(), setName(String) and getId() method:

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.

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();
 }
}

The currentThread() method:
The currentThread() method returns a reference to the currently executing thread object.
output:

<<Previous <<   || Index ||   >>Next >>

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago