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.

Java Multithreading Interview Questions Part – 2

1. 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
   @Override
   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 the Runnable interface,

// defining a thread by implementing Runnable interface
class MyThread implements Runnable {
   // implement run() method
   @Override
   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
   }
}

2. Among these two ways of defining a thread which is better and why?

Defining a thread by implementing the Runnable interface is better and recommended to use.

In the first approach (by extending from 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. 

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

4. Why do we need to call the start() method for starting 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.

5. 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 then thread execution will be started.

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

7. 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. To execute another overloaded form of the run() method we have to call it explicitly like a normal method call. 

class MyThread extends Thread {
   // start() method always call run()
   @Override
   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.

8. What happens if we don’t override the run() method (when the thread is defined by extending from 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 this child thread won’t give any output.

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

class MyThread extends Thread {
   @Override
   public void start() {
      // code
   }
   @Override
   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. 

10. After Starting the Thread if we are restarting the same thread then what happens?*

class MyThread extends Thread {
   @Override
   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 the runtime exception:-java.lang.IllegalThreadStateException. Hence we can’t start a thread more than once.

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

12. How can we get the state of a thread?

We can get the state of a thread by using the getState() method.

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

13. How can we find whether a given thread is a live thread or not?

We find whether a given thread is a live thread or not by 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
   }
}

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

MyThread mt = new MyThread();
System.out.println(mt.getName()); // Thread-0

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

MyThread mt1 = new MyThread();
mt1.setName("KnowProgram");
System.out.println(mt1.getName()); // KnowProgram

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

public class Test {
   public static void main(String[] args) {
      
      Thread t = Thread.currentThread();
      System.out.println(t.getName()); // main
      
      t.setName("KnowProgram");
      System.out.println(t.getName()); // KnowProgram
   }
}

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

18. Can we create multiple threads with the same name?

Yes, we can create multiple threads with the same name.

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

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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