How to create a Thread in Java

In the previous tutorial, we learned about the Checked and Unchecked Exceptions in Java. In this tutorial, we will learn How to create a Thread in Java.

284

Thread is a path of independent execution in a program.

Well, this line doesn’t seem to be much clear. We will understand it better by relating it to the concept of the operating system.

Thread in java with examples
Thread in java with examples

We have a processor in our CPU which is responsible for the execution of all the programs. But, there are many programs that want to be executed by a single processor, while a processor can work on only a single program at a time. To handle this problem, all the processes are connected via a thread to the processor. Then, the processor executes a thread for a limited amount of time, and after some time, it jumps to the next process. This concept is also called scheduling. Due to this, an illusion is created that the processor is simultaneously executing all the tasks, which is actually not possible for a processor to do. It will run only a single program at a time, and the altering is so fast that it seems as if all the tasks are processing at the same time.

Similar things happen in the case of a single Java program. Multiple pieces of code are connected via threads, and these pieces of code execute simultaneously in a time-sharing manner. This is called the concept of threading.

Every thread in Java is created and controlled by a class named java.lang.Thread

How to create a Thread in Java

There are two ways to create a thread in java –

  • Implement the Runnable interface(java.lang.Runnable)
  • By Extending the Thread class(java.lang.Thread)

Implementing Thread using Runnable class –

It is the easiest way to create a thread by implementing Runnable. One can create a thread on any object by implementing Runnable. To implement a Runnable, one has only to implement the run method.

public void run()
We will understand this better with the help of the example given below-
public void run(){
int i;
for(int i=0;i<=10;i++){System.out.println(“Thread A” +i);}
}
Class B implements Runnable{
public void run(){
int i;
for(int i=0;i<=10;i++){System.out.println(“Thread B” +i);}
}
}
public class Example{
public static void main(String[] args){
Thread t1=  new Thread(new A());
Thread t2 = new Thread(new B());
t1.start();
t2.start();
}
}

Output –

Thread B 0
Thread B 1
Thread A 0
Thread B 2
Thread A 1
Thread B 3
Thread A 2
Thread B 4
Thread A 3
Thread B 5
Thread A 4
Thread B 6
Thread A 5
Thread B 7
Thread A 6
Thread B 8
Thread A 7
Thread B 9
Thread A 8
Thread B 10
Thread A 9
Thread A 10

The output is definitely not a pattern, the pieces of codes are randomly run via threads.

Thread Using Thread Class

This is also one of the methods to implement the concept of threading in Java. We can create a child of thread class to do this way.

We will understand this better with the help of the example given below –

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

Output-

Thread is Running

So, in This article, we learned about the concept of threading in Java and implementing it in two different ways.

Try this Quiz

Classes and Objects in Java MCQ Quiz

Previous QuizFundamentals of Object Oriented Programming By Dr. Umesh Baburao Pawar, Sandip University
Next QuizIntroduction to Data Structures and Arrays

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.