Master java skills

How to create a thread in java

In java, threads can be created in two ways:

  1. By extending Thread class
  2. By implementing Runnable interface

1. Creating a thread by extending Thread class

By extending java.lang.Thread class, we can create threads. We need to do two things:

  1. Extend the Thread class
  2. Override public void run() method. Whatever code is written inside this method will be run in a separate thread
package com.sks;

public class MyThread extends Thread {

	@Override
	public void run() {
		System.out.println("This line is printed by a user created thread");
	}
	
	public static void main(String[] args) {
		System.out.println("Main method begins");
		Thread t = new MyThread();
		t.start();
		System.out.println("Main method ends");
	}
	
}

Output:

2. Creating Thread by implementing Runnable interface

In this approach, we will implement Runnable interface and override its only method – public void run(). Following things are required to be done in this case

  1. Write a class that implements java.lang.Runnable interface.
  2. Override run method of Runnable interface
  3. Create an object of the Runnable class (The class that we created in step 1)
  4. Create an object of java.lang.Thread class and pass runnable object (created in step 3) in the constructor of Thread class.
  5. Call start() method on Thread object to begin the thread

Task class

package com.sks;

public class Task implements Runnable {
	
	@Override
	public void run() {
		System.out.println("This line is printed by a user created thread");
	}

}

Main class

package com.sks;

public class ThreadMain {
	
	public static void main(String[] args) {
		
		System.out.println("Main method begins");
		Runnable task = new Task();
		
		Thread t = new Thread(task);
		t.start();
		System.out.println("Main method ends");
	}
}

Output:

The Thread class

java.lang.Thread class is the one whose objects work as threads in java. Objects of any class that extends Thread class also becomes threads. When we call ‘start()’ method on a thread, it start executing the code written inside ‘run()’ method in a separate thread.

The Runnable interface

java.lang.Runnable interface represents a type for objects that can be run in a separate thread by the objects of Thread or its child classes.

This interface has just one method :

public abstract void run ();

Runnable type objects are runnable tasks that can be run in a separate thread.

Difference between Thread and Runnable

ThreadRunnable
Thread is a class, java.lang.ThreadRunnable is an interface, java.lang.Runnale
Its objects represent java threadsObjects of classes that implement this interface represent runnable tasks. Tasks which can be run by threads in separate call stack
It implements Runnable interface. Therefore, Thread is also RunnableIt does not extend any interface
start() method is invoked on thread objectsstart() method is not invoked on runnable objects

Creating multiple threads

In the below example, we are creating 3 threads, namely t1, t2, t3, by extending Thread class

package com.javatrainingschool;

public class UserThread extends Thread {

	@Override
	public void run() {
		System.out.println("UserThread started");
		System.out.println("UserThread finished");
	}
	
	public static void main(String[] args) {
		
		Thread t1 = new UserThread();
		t1.start();
		Thread t2 = new UserThread();
		t2.start();
		Thread t3 = new UserThread();
		t3.start();
	}
}
Output :
UserThread started
UserThread finished
UserThread started
UserThread finished
UserThread started
UserThread finished

In the below example, we are creating 3 threads, using Runnable interface

package com.javatrainingschool;

public class RunnableTask implements Runnable {

	@Override
	public void run() {
		System.out.println("UserThread started");
		System.out.println("UserThread finished");	
	}
	
	public static void main(String[] args) {
		
		RunnableTask task = new RunnableTask();
		Thread t1 = new Thread (task);
		t1.start();
		
		Thread t2 = new Thread (task);
		t2.start();
		
		Thread t3 = new Thread (task);
		t3.start();
	}
}