Creation of thread in java

In a recent post, we have seen what is the thread in java and java thread example. In this post, we will see What is the creation of thread in java. There are two ways to create a java thread by extends the Thread class or implements the Runnable interface. But how does a thread work in memory and how it starts the execution separately?

Here is the table content of the article will we will cover this topic.
1. Creation of thread using thread class
i) Creation of thread by extending thread class in java
ii) Creation of thread by anonymous class
2. Creation of thread using Runnable interface
i) Creation of thread by implementing Runnable interface in java
ii) Creation of thread by anonymous class
iii) Creation of thread by anonymous class(Lambda expression)
3. Creating multiple threads in java

java thread creation

There are two ways to create a thread in java. We can create a thread by use of the Thread class or Runnable interface. But the Creation of a thread in java is always dependent upon the situation and the scenarios. In both ways, we provide the body of the thread in the run() method and start the execution of the thread by the start() method. Let’s discuss how to create a thread in java in different ways:

1. Creation of thread using thread class

We can create a thread in java by using the Thread class. Also we have two ways to create a thread: We can create a thread by extends the Thread class or by use of the anonymous class.

Creation of thread by extending thread class in java

We can create a thread by extends the Thread class that exists in java.lang package. As a result, We need to extend the Thread class, in our class, and create an object/instance of that class. So, We must override the run() method that presents in the Thread class.

Step 1: Create a class in which you want to create a new thread.

Step 2: Extend the Thread class by extends the keyword.

In Step 3: Override the run() method of Thread class and provide the body to run() method. Finally, The body of the run() method is considered as the body of the thread that we want to create.

Step 4: Create the object of the class.

Step 5: Call the start() method to run the execution of the thread.

public class MyThread extends Thread
{
	public static void main(String[] args) 
	{
		MyThread thread1 = new MyThread();
		thread1.start();
	}
	@Override
	public void run()
	{
		System.out.println("My thread is running");
	}	
}

Output: My thread is running

Explanation of above example

Step 1: Here we are creating MyThread class. In which we want to create the thread.

Step 2: Extending the Thread class in MyThread class by extends the keyword. After extending the Thread class, now all the methods of the Thread class present in MyThread class. 

Step 3: In this step, we are overriding the run() method of the Thread class. By overriding, the run() method means we are providing the body to the thread. So, The code present in the run() method executes separately.

Step 4: Creating the object of the MyThread class. It means we are creating the object of thread and now the thread is in a NEW state. We will discuss it lifecycle of the thread.

Step 5: Now thread has been created we just need to start the execution of the thread. To start the execution of the thread we are calling the start() method that present in the Thread class. Finally, The start() method automatically calls the run() method of the current class. We will discuss it later in detail.

Creation of thread by anonymous class

If you are not familiar with the anonymous class in java. Please read it first.

We can create a thread by use of anonymous class in our class. Hence, Here we don’t need to extend the Thread class in our class. We will give the implementation to run method by use of the anonymous class. As we are not inheriting the Thread class, so it means only methods of Thread class will not be available in our class.

Step 1: Create a class in which you want to create a new thread.

Step 2: Create an object of Thread class by use of constructor and also provide the body to run() method. That will be considered as an anonymous class.

Step 3:  Call the start() method to run the execution of the thread.

public class MyThread
{
	public static void main(String[] args) 
	{
		Thread thread = new Thread()
		{
			public void run()
			{
				System.out.println("My thread is running");
			}
		};
		thread.start();
	}
}

Output: My thread is running

Explanation of above example

Step 1: Here we are creating MyThread class. In which we want to create the thread.

Step 2: Creating an anonymous class and overriding the run() method. Here we are providing the body to the thread.

Step 3: Creating the object of the Thread class through an anonymous class. So, It means we are creating the object of thread and now the thread is in a NEW state. Similarly, We will discuss it lifecycle of the thread.

Step 4: Now thread has been created we just need to start the execution of the thread. Also, To start the execution of the thread we are calling the start() method that present in the Thread class. The start() method automatically calls the run() method of the current class. We will discuss it later in detail.

2. Creation of thread using Runnable interface

Another way to create a thread by implementing the Runnable interface. Here we have two ways to create the thread through the Runnable interface. We can create a thread by implements the Runnable interface or by use of the anonymous class.

Creation of thread by implementing Runnable interface in java

We can create a thread by implements the Runnable interface that exists in java.lang package. We need to implement the Runnable interface, in our class. The Runnable interface has only one method that is run() method. In conclusion, We must provide the body with the run() method.

Step 1: Create a class in which you want to create a new thread.

Step 2: Implement the Runnable interface by implements keywords.

Next, Step 3: Provide the body to the run() method of Runnable. The body of the run() method is considered as the body of the thread that we want to create.

Step 4: Create the object of the class that implementing the Runnable interface.

Step 5: Create an object of the Thread class by use of a constructor that takes the object of Runnable types as a parameter. For eg: new Thread(Runnable target)

Step 6: Use the thread object to call the start() method to run the execution of the thread.

public class MyThread implements Runnable 
{
	public static void main(String[] args) 
	{
		MyThread obj = new MyThread();
		Thread thread1 = new Thread(obj);
		thread1.start();
	}

	@Override
	public void run() 
	{
		System.out.println("My thread is running");	
	}
}

Output: My thread is running

Explanation of above example

Step 1: Here we are creating MyThread class. In which we want to create the thread.

Step 2: Implementing the Runnable interface in MyThread class by implements keyword. While Implementing the Runnable interface, we need to implement the run() method in MyThread class.

Step 3: In this step, we are providing the body to the run() method. By implementing the run() method means we are providing the body to the thread. Therefore, The code present in the run() method executes separately.

Step 4: Creating the object of MyThread class that is implementing the Runnable interface.

Step 5: Creating the object of the Thread class and passing the object of MyThread in the constructor of Thread class. Because it takes the object of the Runnable interface.

Step 6: Now thread has been created we just need to start the execution of the thread. Similarly, To start the execution of the thread we are calling the start() method that present in the Thread class. Hence, The start() method automatically calls the run() method of the current class. We will discuss it later in detail.

Creation of thread by anonymous class

If you are not familiar with the anonymous class in java. Please read it first.

We can create a thread by use of anonymous class in our class. In conclusion, we don’t need to implement the Runnable interface in our class. We will give the implementation to run method by use of the anonymous class.

Step 1: Create a class in which you want to create a new thread.

Step 2: Create an object of the Runnable interface by providing the anonymous implementation of the interface. Another, Provide the body to run() method and that will be considered as an anonymous class.

Step 3: Create an object of the Thread class and pass the object of Runnable in the constructor of the Thread class.

Step 4:  Call the start() method to run the execution of the thread.

public class MyThread 
{
	public static void main(String[] args) 
	{
		Runnable runnable = new Runnable() 
		{
			@Override
			public void run() 
			{
				System.out.println("My thread is running");
			}
		};
		Thread thread1 = new Thread(runnable);
		thread1.start();
	}
}

Output: My thread is running

Creation of thread by anonymous class(Lambda expression)

Please read the functional interface and lambda expression first.

As we know the Runnable interface is a functional interface because it has only one method. So, we can use the lambda expression to implement it. By use of lambda expression, we can reduce the code and make it more readable.

public class MyThread 
{
	public static void main(String[] args) 
	{
		Runnable runnable = () -> System.out.println("My thread is running");
		Thread thread1 = new Thread(runnable);
		thread1.start();
	}
}

Output: My thread is running

Creating multiple threads in java

We can create multiple thread in java program. So, Let’s see how to create multiple thread.

class MyThread implements Runnable 
{
	String name;
	Thread thread;
	MyThread(String threadName)
    {
	    name = threadName; 
	    thread = new Thread(this, name);
	    thread.start();
    }
	public void run() 
	{
		 try 
		 {
		     for(int i = 0; i < 5; i++) 
		     {
		    	 System.out.println("The "+ name + " is running...");
		    	 Thread.sleep(1000);
		     }
		 }
		 catch (InterruptedException e) 
		 {
			 System.out.println(name + "Interrupted");
		 }
	}
}
 
public class MultipleThread 
{
	public static void main(String args[]) 
	{
	     new MyThread("First thread");
	     new MyThread("Second thread");
	     new MyThread("Third thread");
	     try 
	     {
	    	 Thread.sleep(10000);
	     } 
	     catch (InterruptedException e) 
	     {
	    	 System.out.println("Main thread Interrupted");
	     }
	 }
}

Output: The First thread is running…
The Third thread is running…
The Second thread is running…
The Third thread is running…
The Second thread is running…
The First thread is running…
The First thread is running…
The Second thread is running…
The Third thread is running…
The Third thread is running…
The First thread is running…
The Second thread is running…
The First thread is running…
The Second thread is running…
The Third thread is running…

NOTE: The output of the above program can vary because we can’t predict the output of multiple threads if they are running parallelly.  In the above example, you have seen we are creating a class MyThread that implementing the Runnable interface and also providing the body to run() method.

Line 1: Here we are creating the MyThread class that implements the Runnable interface.

Line 5: Creating the constructor of MyThread that is used to create a new thread.

Also, Line 7: Assigning the name of the thread.

Line 8: Creating a new thread and providing the name to the thread.

Line 9: To start the execution of the thread calling the start() method.

Another in Line 11: Here we are providing the body to run method.

Line 15-19: The thread will execute the statement written on line 17. After that the thread will go to sleep for 1 second, meanwhile, another thread will enter the loop and execute the statement, and so on.  

Line 28: We are creating another class that is used to invoke the constructor of MyThread class.

Line 32 – 34:  Here we are calling the constructor of MyThread class and providing the names of thread.

how to create multiple thread in java

2 thoughts on “Creation of thread in java”

Leave a Comment