The perfect place for easy learning...

Java Programming

×

Topics List


Creating threads in java





In java, a thread is a lightweight process. Every java program executes by a thread called the main thread. When a java program gets executed, the main thread created automatically. All other threads called from the main thread.

The java programming language provides two methods to create threads, and they are listed below.

  • Using Thread class (by extending Thread class)
  • Uisng Runnable interface (by implementing Runnable interface)

Let's see how to create threads using each of the above.

Extending Thread class

The java contains a built-in class Thread inside the java.lang package. The Thread class contains all the methods that are related to the threads.

To create a thread using Thread class, follow the step given below.

  • Step-1: Create a class as a child of Thread class. That means, create a class that extends Thread class.
  • Step-2: Override the run( ) method with the code that is to be executed by the thread. The run( ) method must be public while overriding.
  • Step-3: Create the object of the newly created class in the main( ) method.
  • Step-4: Call the start( ) method on the object created in the above step.

Look at the following example program.

Example
class SampleThread extends Thread{
	
	public void run() {
		System.out.println("Thread is under Running...");
		for(int i= 1; i<=10; i++) {
			System.out.println("i = " + i);
		}
	}	
}

public class My_Thread_Test {

	public static void main(String[] args) {
		SampleThread t1 = new SampleThread();
		System.out.println("Thread about to start...");
		t1.start();
	}
}

When we run this code, it produce the following output.

creating thread in java uisng Thread class

Implementng Runnable interface

The java contains a built-in interface Runnable inside the java.lang package. The Runnable interface implemented by the Thread class that contains all the methods that are related to the threads.

To create a thread using Runnable interface, follow the step given below.

  • Step-1: Create a class that implements Runnable interface.
  • Step-2: Override the run( ) method with the code that is to be executed by the thread. The run( ) method must be public while overriding.
  • Step-3: Create the object of the newly created class in the main( ) method.
  • Step-4: Create the Thread class object by passing above created object as parameter to the Thread class constructor.
  • Step-5: Call the start( ) method on the Thread class object created in the above step.

Look at the following example program.

Example
class SampleThread implements Runnable{
	
	public void run() {
		System.out.println("Thread is under Running...");
		for(int i= 1; i<=10; i++) {
			System.out.println("i = " + i);
		}
	}	
}

public class My_Thread_Test {

	public static void main(String[] args) {
		SampleThread threadObject = new SampleThread();
		Thread thread = new Thread(threadObject);
		System.out.println("Thread about to start...");
		thread.start();
	}
}

When we run this code, it produce the following output.

creating thread in java uisng Runnable interface

More about Thread class

The Thread class in java is a subclass of Object class and it implements Runnable interface. The Thread class is available inside the java.lang package. The Thread class has the following syntax.

class Thread extends Object implements Runnable{
...
}

The Thread class has the following consructors.

  • Thread( )
  • Thread( String threadName )
  • Thread( Runnable objectName )
  • Thread( Runnable objectName, String threadName )

The Thread classs contains the following methods.

Method Description Return Value
run( ) Defines actual task of the thread. void
start( ) It moves thre thread from Ready state to Running state by calling run( ) method. void
setName(String) Assigns a name to the thread. void
getName( ) Returns the name of the thread. String
setPriority(int) Assigns priority to the thread. void
getPriority( ) Returns the priority of the thread. int
getId( ) Returns the ID of the thread. long
activeCount() Returns total number of thread under active. int
currentThread( ) Returns the reference of the thread that currently in running state. void
sleep( long ) moves the thread to blocked state till the specified number of milliseconds. void
isAlive( ) Tests if the thread is alive. boolean
yield( ) Tells to the scheduler that the current thread is willing to yield its current use of a processor. void
join( ) Waits for the thread to end. void

🔔 The Thread class in java also contains methods like stop( ), destroy( ), suspend( ), and resume( ). But they are depricated.