Define Thread in Java Multithreading Interview Questions

Java Multithreading Interview Questions Part – 2 | In the previous Java Multithreading Interview Questions Part – 1 we have seen interview questions related to the Introduction of multithreading. In Java, multithreading is one of the most important areas to ask questions in the interview therefore we have prepared many questions and answers on Java multithreading. Since it becomes a long article so we divided it into different parts.

Q1) What are the different ways to define a thread in Java?
We can define a thread in the following 2 ways,

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

Defining a thread in Java by extending from Thread class,

// defining a thread extending from Thread class
class MyThread extends Thread {
   // override run() method
   public void run() {
      // code
   }
}
// Testing
public class ThreadDemo {
   public static void main(String[] args) {
      // thread instantiation
      MyThread t = new MyThread();
      t.start(); // child thread start
   }
}

Defining a thread by implementing a Runnable interface,

// defining a thread by implementing Runnable interface
class MyThread implements Runnable {
   // implement run() method
   public void run() {
      // code
   }
}
// Testing
public class ThreadDemo {
   public static void main(String[] args) {
      MyThread mt = new MyThread();
      MyThread t = new MyThread(mt);
      t.start(); // child thread start
   }
}

Q2) Among these two ways of defining a thread which is better and why?
Defining a thread by implementing a Runnable interface is better and recommended to use.

In the first approach (by extending Thread class) our class already extends from the Thread class therefore there is no chance of extending it from any other class. In this way, we will miss inheritance benefits. 

In the 2nd way (by implementing Runnable interface) our class can extend from any class. And due to this reason, we will have inheritance benefits. Therefore in these two ways, the 2nd way i.e. “implementing from Runnable interface” is recommended. The 1st way also gives performance issues. 

Q3) If we want to start a thread then which method must be called start() or run() method?
The start() method. If we call the run() method then it will be executed as a normal method called, and a new thread will not be created.

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Q4) Why do we have to call the start() method to start a new thread?
Thread class start() method is responsible for,

  1. Register the thread with thread scheduler.
  2. Perform all other mandatory activities.
  3. Invoke run() method.

Hence, without executing the thread class start() method there is no chance of starting a new thread in Java.

Q5) When we call the start() method on a Thread object, will thread execution start immediately?

No, the start() method has to perform some important activities like- register the thread, perform all other mandatory activities. After completing these operations it will invoke the run() method, and thread execution will be started.

Q6) What is the difference between t.start() and t.run()?***

MyThread t = new MyThread();
t.start();
t.run();

In the case of the t.start() method, a separate thread will be created which will be responsible for the execution of the run() method. But in the case of the t.run() method, it will be a normal method call. A new thread will not be created, and the run() method will be executed by the main thread itself.

Q7) Can we overload the run() method?

Yes, overloading of the run() method is possible but the Thread class start() method always invokes the run() method with no argument only. To execute another overloaded form of the run() method we have to call it explicitly like a normal method call. 

class MyThread extends Thread {
   public void run() {
      // call other run(-) method from here
      run(7.5);
   }
   public void run(double n) {
      // code
   }
}

Note:- Overloading of the main() method is also possible but the main thread always calls:- “public static void main(String[] args)” method. Other overloaded forms of main() will be treated like normal methods, and to execute them we have to call them explicitly.

Q8) What happens if we don’t override the run() method (when the thread is defined by extending the Thread class)?

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

Since the run() method is not available in the child class (MyThread) therefore it will be executed from the parent class (Thread class). The run() method of the Thread class has an empty implementation. Therefore the program will run successfully but we won’t get any output.

Q9) What happens if we override the start() method?***

class MyThread extends Thread {
   public void start() {
      // code
   }
   public void run() {
      // code
   }
}

Since we are overriding the start() method in MyThread class, therefore the start() method of the Thread class will not get a chance to execute. In this case, the start() method of MyThread will be executed like a normal method call. A new thread will not be created. Complete execution will be done by only the main thread, therefore output will be fixed. 

Q10) After Starting the Thread if we are restarting the same thread then what happens?*** 

class MyThread extends Thread {
   public void run() {
      // code
   }
}
public class ThreadDemo {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      t.start(); 
      // some code
      t.start(); 
   }
}

After starting a thread if we are trying to restart the same thread then we will get a runtime exception:-java.lang.IllegalThreadStateException. Hence we can’t start a thread more than once.

Q11) Explain the life cycle phases of a thread?

  • NEW:- Thread has been created but not yet started. Thread will be started after the start() method call.
  • RUNNABLE:- Thread is in running state, and currently executing.
  • TIMED_WAITING:- Thread was in running state, and now it is waiting for a certain fixed amount of the time. After that time period, the thread scheduler decides which thread will get a chance to execute.
  • WAITING:- Thread was in running state, and now it is waiting for its turn. The thread scheduler decides which thread will get a chance to execute.
  • BLOCKED:- Thread got blocked for some reasons.
  • TERMINATED:- Thread execution completed.

Q12) How can we get the state of a thread?
Using the getState() method.

MyThread mt = new MyThread();
System.out.println(mt.getState()); // NEW
mt.start();
System.out.println(mt.getState()); // RUNNABLE

Q13) How can we find whether a given thread is a live thread or not?
Using isAlive() method.

class MyThread extends Thread {}
public class Test {
   public static void main(String[] args) throws Exception {
      MyThread mt = new MyThread();
      System.out.println(mt.isAlive()); // false
      mt.start();
      System.out.println(mt.isAlive()); // true
      Thread.sleep(2000);
      System.out.println(mt.isAlive()); // false
   }
}

Q14) How can we get the currently running thread reference?
Thread.currentThread() method. The currentThread() method of the Thread class is a static method that returns a reference to the currently running thread.

Q15) How can we get the name of a thread?
Using getName() method. The getName() method defined in the Thread class returns the name of the thread.

Q16) How can we set the name of a thread?
Using setName() method. The setName(String str) method defined in the Thread class sets the name of the given thread.

Q17) What is the default name of the child thread?
By default, JVM assigns the name of the child thread as thread-0, thread-1, thread-2, and so on.

Q18) Can we create multiple threads with the same name?
Yes, we can.

MyThread mt = new MyThread();
mt.setName("KP");
mt.start();
System.out.println(mt.getName()); // KP
MyThread mt1 = new MyThread();
mt1.setName("KP");
mt1.start();
System.out.println(mt.getName()); // KP
Join PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Leave a Comment

Your email address will not be published. Required fields are marked *