Java – Defining a Thread by Extending Thread Class | Code Factory


Index Page : Link

Donate : Link

Medium Link : Link

Applications : Link

package com.example.thread;

public class ThreadTest {
	public static void main(String... args) {
		MyThread t = new MyThread(); // Thread instantiation
		t.start(); // starting a Thread
		// executed by Main Thread (line no. 8 to 10)
		for(int i=0; i<5; i++) {
			System.out.println("Main Thread");
		}
	}
}

// Define a Thread (line no. 15 to 22)
class MyThread extends Thread {
	public void run() {
		// job of Thread, executed by Child Thread (line no. 18 to 20)
		for(int i=0; i<5; i++) {
			System.out.println("Child Thread");
		}
	}
}

Output :

Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread

. . . . .

Thread Schedular :

  • It is the part of JVM
  • It is responsible for to schedule threads, that is if multiple threads are waiting to get chance of execution then in which order threads will be executed is decided by Thread Schedular.
  • We can’t expect exact algorithm followed by thread schedular, it is varied from JVM to JVM, Hence we can’t expect thread execution order and exact output.
  • Hence whenever situation come to multithreading there is no guarantee for exact output but we can provide several possible outputs.

. . . . .

Difference between t.start() and t.run() :

  • In the case of t.start(), a new Thread will be created which is responsible for the execution of run method.
  • But in the case of t.run() a new Thread wouldn’t be created and run method will be executed just like a normal method call by main Thread.
  • Hence in the above program if we replace t.start() with t.run() then output is child thread 10 times followed by main thread 10 times.

. . . . .

Importance of Thread class start method :

  • Thread class start method is responsible to register the thread with the thread schedular and all other mandatory activities.
  • Hence without executing Thread class start() method there is no chance of starting a new Thread in Java. Due to this Thread class start() method is consider as Heart of multitasking.
start() {
   1. Register this thread with thread schedular
   2. Perform all other mandatory activities
   3. Invoke run()
}

. . . . .

Overloading of run() method :

  • Overloading of run() method is always possible but Thread class start method can invoke no argument run() method. The other overloaded method we have to call explicitly like a normal method call.

. . . . .

If we are not overriding run method :

  • If we are not overriding run() method then Thread class run() method will be executed which has empty implementation. Hence we wouldn’t get any output.
  • It is highly recommended to override run method otherwise don’t go for multithreading concept.

. . . . .

Overriding of start() method :

  • If we override start() method then our start() method will be executed just like normal method code and new thread wouldn’t be created.
  • It is not recommended to override start() method otherwise don’t go for multithreading concept.

. . . . .

Life Cycle of Thread :

. . . . .

IllegalThreadStateException :

  • After starting a thread if we are trying to restart the same thread then we will get runtime exception saying IllegealThreadStateException
Thread t = new Thread();
t.start();
   .
   .
   .
t.start();

2 thoughts on “Java – Defining a Thread by Extending Thread Class | Code Factory”

Leave a comment