2. Multi-threading in Java using Runnable

In previous chapter, we have seen how to use Thread (java.lang.Thread) class to create thread in Java language. In this chapter we will see how to use Runnable (java.lang.Runnable) Interface to create thread in Java language.

1. Runnable Interface

  1. Runnable interface is present in java.lang package.
  2. Only one method is declared inside Runnable interface.
  3. Method declaration: public void run();
  4. As Runnable is interface, we need help of Thread.start() method to start the thread, while creating Thread using Runnable interface. We will see how to do it.

2. Using Runnable Interface to create Thread.

Check program compilation and execution here! jDoodle

3. Two ways of Thread creation in Java.

Types of Thread creations in Java - Core Java Tutorial webencyclop.com

4. Different combinations of t.start() and t.run() methods.

Consider below program and let’s see the what each line does.

Case I (line 19): t1.start()

New Thread will be created, which is responsible for execution of run() method present in our MyRunnable class. This is usually the correct way to create Thread using Runnable interface.

Case II (line 20): t2.start()

New Thread will be created, which is responsible for execution of run() method present our Thread class. While creating t2 object we didn’t pass the target Runnable object, hence it called run() method from Thread class instead of run() method from our class.

Case III (line 22): t1.run()

New thread will not be created, but run() method will be called from MyRunnable class. (Only Thread.start() method is responsible for creating new Thread as we have seen in previous chapter.)

Case IV (line 23): t2.run()

New thread will not be created, but run() method will be called from Thread class. run() method in Thread class has blank implementation, so nothing will print when it executes. (Only Thread.start() method is responsible for creating new Thread as we have seen in previous chapter.)

Case V (line 25): r.start()

It will give Compile time error.
error: cannot find symbol | symbol: method start() | location: variable r of type MyRunnable | 1 error

Case V (line 26): r.run()

New Thread will not be created. But run() method present in Runnable class will be executed just like normal method call.

In second point, we have covered how to create Thread using Runnable interface, now let’s see which is better way to create the Thread and why? See you in next chapter.

Help others by sharing the content!

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!