Master java skills

Creating first java thread

We have already talked about the default thread ‘main’ on previous page. Here we will learn how to create user threads within a java program. There are two ways of doing it

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

1. Creating thread using Thread class

In the below example, we will create a thread using java.lang.Thread class.

package com.javatrainingschool;

public class MyFirstThread {
	
	public static void main(String[] args) {
		
		//Creating a thread with name 'My First Thread'
		Thread t1 = new Thread("My First Thread");
		t1.start();
		//printing the name of the thread
		System.out.println(t1.getName());
	}
}
Output :
My First Thread

2. By extending Thread class

By extending java.lang.Thread class, we can create threads. Let’s see an example

In this approach, the class that extends java.lang.Thread class becomes a Thread type. Please note that, we have to override public void run() method here.

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 t = new UserThread();
		t.start();
	}
	
}
Output :
UserThread started
UserThread finished

3. 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

Let’s see one example

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 t = new Thread (task);
		t.start();
	}
}
Output :
UserThread started
UserThread finished

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();
	}
}