1. Multi-threading Concept in Java – Detailed

Note: This tutorial is intended for users who has basic idea about the multi-threading concept (like what is thread etc). In this tutorial we will see the detailed concept of multi-threading in Java.

  1. Purpose: The main purpose of having multi-threading concept in java is to improve the performance and reduce the response time. Multiple threads can perform activity concurrently so, it improves the performance.
  2. Developing multi-threaded applications in Java is easy because Java provides multi-threading support by using rich APIs which are really easy to use. But for this you will need to go through the entire tutorial to understand the basic and advance concepts in the Java multi-threading

Ways to define a Thread in Java

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


1. Using Thread class.

Check the code compilation at: jDoodle

2. What is Thread Scheduler?

  • It is the part of JVM (Java Virtual Machine), it is responsible to schedule Threads.
  • If multiple Threads are waiting to get the chance if execution, then in which order Threads will be executed is decided by Thread Scheduler
  • We can’t tell the exact algorithm followed by Thread Scheduler, it varies from JVM to JVM. (JVM is different for different platforms.)
  • Hence we can’t expect the execution order and exact output. As you have seen the output of above program, that it varies during almost every execution. Here jDoodle (try executing multiple time..you will see the difference in output.)
  • Hence whenever situation comes to multi-threading there is no guarantee for exact output, but we can provide several possible outputs.

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

  • In case of, t.start() => A new Thread will be created, which will be responsible for execution of run() method
  • In case of, t.run() => Same calling thread (main-thread) will excute run() method, just like normal method call. New thread will not be created.

4. Importance of Thread Class’s t.start() method.

  • It is responsible to register Thread with Thread Scheduler and all other mandatory activities required for creating Thred on that platform.
  • Hence without executing this start() method, there is no chance of starting new Thread in Java.
  • Hence start() method of Thread class is considered as Heart of Multi-threading.

5. Overriding t.start() method.

  • If we override start() method in Thread class, the our start() method will be executed just like normal method call. No new Thread will be created.
  • For multi-threading to work, start() method of Thread class (Parent class) must be called which has code for creating the Thread. So, we must use super.start() inside our overridden start() method, that will perform the task of creating the Thread for us.
  • Note: It is not at all recommended to override the start() method in Thread class.

6. Thread Life Cycle

Life cycle of a thread - Java - WebEncyclop

7. What will happen when we restart already started Thread?

  • After starting the Thread, if we are trying to restart the Thread, the we will get Runtime Exception
  • Exception: IllegalThreadStateException
  • Check the execution of below program: jDoodle

In next chapter, we will see how to create Thread using Runnable interface, and methods present in Runnable interface.
We will also cover more details about Thread class in 3rd Chapter: constructors in Thread class and available methods in Thread class.

Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.