Thursday, 9 December 2021

Creating Threads in Java

 In java a threads are objects and can be created in two ways.

1. By extending the thread class

2. By implementing Runnable interface

In the  first approach, a user specified thread class is created by extending the thread class and overriding it's run() method. In the second approach, a thread is created by implementing the Runnable interface and overriding it's run() method. In both approaches the run() method has to be overridden. Among these two methods creating a thread by extending Runnable interface is more reliable and efficient, because when your extending a thread class it limits the subclass to inherit only the superclass features.

Creating a new thread by extending thread class

The first method of creating thread is by extending the thread class. The thread class is defined in the package java.lang. The class that inherits , will override the run() method of the parent class for it's implementation

The following java program illustrates about how to create a thread by using thread class

import java.lang.*;
class ThreadExample extends Thread 
{
	public ThreadExample(String name)
	{
		super(name); // calling super class constructor from child class constructor to get it's name
	}

public void run()
{
System.out.println(Thread.currentThread()); // printing default thread details 
for(int i=0;i<5;i++)
{
	System.out.println(i);
}
}
}
class ExampleT
{
	public static void main(String args[])
	{
          ThreadExample t1=new ThreadExample("First");
		  t1.start();
		  System.out.println("This is" +Thread.currentThread()); // printing user defined thread details 
	}
}


Output of the above program is

D:\java examples>javac ExampleT.java
D:\java examples>java ExampleT
This is Thread[main,5,main]
Thread[First,5,main]
0
1
2
3
4

Creating a thread by implementing Runnable interface 

The interface Runnable is defined in the java.lang package. It has a single method run(). The majority of classes created need to be run as thread will implements the Runnable interface. Since they be extending some other functionality from other classes.


The following program illustrates how to implement Runnable interface to create new thread 

import java.lang.*;
class ThreadExample implements Runnable
{
public void run()
{
System.out.println("starting user defined thread");  
for(int i=0;i<5;i++)
{
	System.out.println(i);
}
System.out.println("user defined thread ends here"); 
}
}
class ExampleR
{
	public static void main(String args[])
	{
          ThreadExample t1=new ThreadExample();
		  Thread th = new Thread(t1);
		  th.start();
		  System.out.println("This is" +Thread.currentThread()); 
		  System.out.println("End of main thread"); 
	}
}


Output:-

D:\java examples>javac ExampleR.java
D:\java examples>java ExampleR
This is Thread[main,5,main]
End of main thread
starting user defined thread
0
1
2
3
4
user defined thread ends here

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

Computer Basics

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top