Defining a Thread in java by extending Thread class.

Defining a thread :

We can define a thread in the following two ways :
a. By extending Thread class
b. By implementing runnable interface.

By extending Thread class.

After extending a thread it is necessary to override the run method of the Thread class to define a job of the class.

package com.demo1;

import java.util.stream.IntStream;

public class MyThread extends Thread{ //Defining a Thread

	public void run() { //Job of a Thread
		IntStream.range(1,10).forEach(x -> System.out.println("Child Loop : "+x));
	}
}
class DemoRunningThread{
	public static void main(String[] args) { //Main Thread
		MyThread myThread = new MyThread(); //Thread instantiation
		myThread.start(); //Starting of a Thread
		IntStream.range(1,10).forEach(x -> System.out.println("Main Loop : "+x));
	}
}

Case 1 : Thread Scheduler

Thread Scheduler is a part of JVM, it is responsible to schedule threads If multiple threads are waiting to be executed then in which order they will be executed will be decided by Thread scheduler.The algorithm (SJF,FCFS,Round Robin) used by Thread scheduler differs from JVM to JVM. The output of the above program is not fixed it changes from JVM to JVM or from execution to execution, we cannot define which will be printed first.

Case 2 : Difference between thread.start() and thread.run()

In case of thread.start() a new thread will be created which will be responsible to execute out job but in case of thread.run() our run method will be executed like a normal method and a new thread will not be created.

Case 3 : Importance of Thread class start method

Thread class start method is responsible to register the thread with thread scheduler and all other mandatory activities. Hence without executing thread class start method there is not chance of starting a new thread in java due to this thread class start method is considered as heart of multi-threading.
Steps of Thread class’s start method
a. Register the thread with the Thread Schedular
b. Perform all other mandatory action.
c. Invoke the run method.

Case 4 : Overloaded run method

Overloading of run method is possible but thread.start() method will always call the empty parameter run() run method.Other overloaded run method with any number of arguments should be called explicitly.

Case 5 : Not overriding run method

We can have a class extend Thread class but not override the run method and still execute the program without any compilation or run time error because the Thread class has a run() method in its class with an empty implementation. It is highly recommended to override run method other wise it is better to not implement run method.

Case 6 : Overriding of start method.

In case when a class extends a thread class but also overrides the start() method then the Thread class’s start() method will not be called and a new thread will not be created. Just the contents of start() method of the child class which is extending the Thread parent class will be called.

Case 7 : super.start()

If super.start() method is called in the child class it can execute the start() method of the actual Thread class which will create the thread

package com.demo1;

import java.util.stream.IntStream;

public class MyThread extends Thread{ //Defining a Thread

	public void start() {
		super.start(); //Starting the Thread
		System.out.println("Start method");
	}
	
	public void run() { //Job of a Thread
		IntStream.range(1,10).forEach(x -> System.out.println("Child Loop : "+x));
	}
}
class DemoRunningThread{
	public static void main(String[] args) { //Main Thread
		MyThread myThread = new MyThread(); //Thread instantiation
		myThread.start(); //calling the start method of MyThread and not Thread class
		IntStream.range(1,10).forEach(x -> System.out.println("Main Loop : "+x));
	}
}

Case 8 : Thread Lifecycle

Note : Once you have started a thread using thread.start() method and again if you try to start the method we get exception IllegalThreadStateException

Leave a Comment