Instantiating and Starting the Thread

Instantiating and Starting the Thread

We can define instantiate and starting a thread by using the following 2 ways.

1. By extending Thread Class

E.g

class MyThread extends Thread {
   public void run() {
      for (int i = 0; i < 5; i++) {
         System.out.println("Child Thread");
      }
   }
}

public class MultiThreadDemo {
   public static void main(String[] args) throws InterruptedException { 
      MyThread t = new MyThread();
      t.start();
      for (int i = 0; i < 5; i++) {
         System.out.println("Main Thread");
      }
   }
}

Case1: Thread Scheduler

If multiple threads are there then which thread will get chance first for execution will be decided by β€œThread Scheduler”. Thread Scheduler is the part of JVM. The behavior of thread scheduler is vendor dependent and hence we can’t expect exact O/P for the above program.

The possible Outputs are

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

Case 2: Difference between t.start() and t.run() methods.

  • 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() no new Thread will be created and run() method will be executed just like a normal method by the main Thread.
  • In the above program if we are replacing t.start() with t.run() the following is the output.
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread

Case 3: Importance of Thread Class start() method

After Creating thread object compulsory we should perform registration with the thread scheduler. This will take care by start() of Thread class, So that the programmers has to concentrate on only job. With out executing Thread class start() method there is no chance of start a new Thread in java.

start() {
   Register our thread with the thread scheduler.
   Invoke run method.
}

Case 4: 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 and hence we won’t get any output.

class MyThread extends Thread {

}

class ThreadDemo {
   public static void main(String[] args)  {
      MyThread t=new MyThread();
      t.start();
   }
}

Output
No output

It is highly recommended to override run() method. Otherwise don’t go for multithreading concept.

Case 5: Overloding of run() method.

We can overload run() method but Thread class start() method always invokes no argument run() method the other overload run() methods we have to call explicitly then only it will be executed just like normal method.

class MyThread extends Thread {
   public void run() {
      System.out.println("Inside no arg method");
   }
   public void run(int i) {
      System.out.println("Inside int arg method");
   }
}

class ThreadDemo {
   public static void main(String[] args) {
      MyThread t=new MyThread();
      t.start();
   }
}

Output:
Inside no arg method

Case 6: Overriding of start() method

If we override start() method then our start() method will be executed just like a normal method call and no new Thread will be started.

class MyThread extends Thread {
   public void start() {
      System.out.println("Inside start method");
   }
   public void run() {
      System.out.println("Inside run method");
   }
}

class ThreadDemo {
   public static void main(String[] args) {
      MyThread t=new MyThread();
      t.start();
      System.out.println("Inside main method");
   }
}

Output:
Inside start method
Inside main method

Case 7: Life cycle of the Thread

  • Once we created a Thread object then the Thread is said to be in new state or born state.
  • Once we call start() method then the Thread will be entered into Ready or Runnable state.
  • If Thread Scheduler allocates CPU then the Thread will be entered into running state.
  • Once run() method completes then the Thread will entered into dead state.

Case 8

After starting a Thread we are not allowed to restart the same Thread once again otherwise we will get runtime exception saying “IllegalThreadStateException”.

class MyThread extends Thread {
   public void run() {
      System.out.println("Hey this is run method");
   }
}

class ThreadDemo {
   public static void main(String[] args)  {
      MyThread t=new MyThread();
      t.start();
      System.out.println("Hi main method"); // Fine
      t.start();
      System.out.println("Bye main method"); //we will get R.E saying: IllegalThreadStateException
   }
}
2. By Implementing Runnable Interface

We can define a Thread by implementing runnable interface also. Runnable interface available in java.lang package and contains only one method

public void run();
class MyRunnable implements Runnable {
   public void run() {
      for (int i = 0; i < 5; i++) {
         System.out.println("Child Thread");
      }
   }
}
 
public class MultiThreadDemo {
   public static void main(String[] args) throws InterruptedException {
      MyRunnable r = new MyRunnable();
      Thread t = new Thread(r);
      t.start();
      for (int i = 0; i < 5; i++) {
         System.out.println("Main Thread");
      }
   }
}

Possible Output

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

E.g

MyRunnable r = new MyRunnable();
Thread t1 = new Thread();
Thread t2 = new Thread(r);

Case 1: t1.start();

A new thread will be started and executes thread class run method(Which is having empty implementation).

Case 2 t1.run();

No new thread will be created and thread class run() method will be executed just like a normal method call.

Case 3: t2.start();

A new thread will be created and responsible for execution of MyRunnable run method.

Case 4: t2.run();

No new thread will be created and MyRunnable run method will be executed just like a normal method call.

Case 5: r.run();

No new thread will be created and MyRunnable run method will be executed just like a normal method call.

Case 6: r.start();

Compiler Error: MyRunnable doesn’t contain start method.

Note

Among 2- approaches of defining a thread – implements Runnable is always recommended to use in the 1st approach as we are already extending thread class. There is no chance of extending any other class. Hence we are missing the key benefits of oops inheritance(reusability). Hence this approach is not recommended to use.

Hybrid Mechanism To define a thread (not recommended to use)
class MyThread extends Thread {
   public void run() {
      System.out.println("Child Thread");
   }
}

class HybridThreadDemo {
   public static void main(String arg[]) {
      MyThread t1 = new MyThread();
      Thread t = new Thread(t1);
      t.start();
      System.out.println("Main Thread");
   }
}

Output

Child Thread
Main Thread
Or
Main Thread
Child Thread
Thread class construtors
Thread t = new Thread();
Thread t = new Thread(Runnable r);
Thread t = new Thread(String name);
Thread t = new Thread(Runnable r, String name);
Thread t = new Thread(ThreadGroup g, String name);
Thread t = new Thread(ThreadGroup g, Runnable r);
Thread t = new Thread(ThreadGroup g, Runnable r, String name);
Thread t = new Thread(ThreadGroup g, Runnable r, String name, long stacksize);
Setting and Getting the name of a Thread

Every Thread in java has some name it may be provided explicitly by the programmer or automatically generated by JVM. Thread class defines the following methods to set and get the name of a Thread.

  • public final void setName(String name);
  • public final String getName();
public class Test {
   public static void main(String arg[]) {
      System.out.println(Thread.currentThread().getName());
      Thread.currentThread().setName("My Main Thread");
      System.out.println(Thread.currentThread().getName());
   }
}

Output

main
My Main Thread
Instantiating and Starting the Thread


Scroll to top