In this article, we will discuss 2 important methods of Thread class – start() & run()
1. Thread methods :
There are several methods in Thread class, let’s focus on 2 important methods –
- run()
- If the invoking thread was constructed using a separate Runnable run object, then that Runnable object‘s run method is called; otherwise, this method does nothing and returns
- start()
- Causes the invoking thread to begin execution; the Java Virtual Machine (JVM) calls the run method of invoking thread
2. Thread class design :
- Thread class implements Runnable interface and overrides run() method providing dummy implementation
- Thread class has several methods and one such method is start() which is used to start/begin the execution of the newly created/spawned Thread
- There are around 7K lines of code (LoC) inside start() method and core activities include –
- Register a Thread with Thread Scheduler (TS) so that TS allocates CPU/processor
- Perform mandatory activities required to start the newly created/spawned Thread
- Invoke run() method
- So, whenever start() method is invoked on newly created Thread object then it does above listed activities before finally invoking run() method
- Trivia: for programmer it looks like whenever start() method is invoked run() method’s implementation is executed
3. Creating or Spawning a New Thread :
- There are 2 different ways to create/spawn a new Thread –
- Extending Thread class
- Implementing Runnable interface
- In both the ways, instantiating Thread class is must to create a new Thread as shown in the below illustration
MyNewThread.java
package in.bench.resources.threads;
public class MyNewThread extends Thread {
@Override
public void run() {
// work for new thread (Thread-0)
for(int i=0; i<10; i++) {
System.out.println("New Thread - i = " + i);
}
}
public static void main(String[] args) {
// create a new Thread
MyNewThread t = new MyNewThread();
// starting new Thread
t.start(); // this will invoke MyNewThread's run() method
// work for main thread (main)
for(int j=0; j<10; j++) {
System.out.println("main Thread - j = " + j);
}
}
}
MyNewRunnable.java
package in.bench.resources.threads;
public class MyNewRunnable implements Runnable {
@Override
public void run() {
// work for new thread (Thread-0)
for(int i=0; i<10; i++) {
System.out.println("New Thread - i = " + i);
}
}
public static void main(String[] args) {
// create a new Thread
MyNewRunnable tRun = new MyNewRunnable();
// instantiate Thread object & pass runnable object
Thread t = new Thread(tRun);
// starting new Thread
t.start(); // this will invoke MyNewRunnable's run() method
// work for main thread (main)
for(int j=0; j<10; j++) {
System.out.println("main Thread - j = " + j);
}
}
}
4. Starting newly created Thread :
- First step is to create a new Thread class either by extending Thread class or implementing Runnable interface overriding run() method providing implementation details
- Next step is to instantiate newly created Thread class as shown in the above section
- Final step is to invoke Thread class’ start() method which will start the execution of new Thread or separate flow of execution is started for new Thread from parent Thread (or main Thread)
- Although, start() method is invoked but JVM internally calls/invoke run() method’s implementation as detailed in the Thread class design in the above section 2
- Instead of invoking Thread class’ start() method, if Thread class’ run() method is invoked then new separate independent flow of execution or new Thread won’t be created and run() method runs under main Thread as any other normal method as shown in the below illustration
MyNewThread.java
package in.bench.resources.threads;
public class MyNewThread2 extends Thread {
@Override
public void run() {
// work for new thread (Thread-0)
for(int i=0; i<10; i++) {
System.out.println("New Thread - i = " + i);
}
}
public static void main(String[] args) {
// create a new Thread
MyNewThread2 t = new MyNewThread2();
// starting new Thread
t.run(); // this will be executed under parent main Thread
// work for main thread (main)
for(int j=0; j<10; j++) {
System.out.println("main Thread - j = " + j);
}
}
}
Output :
New Thread - i = 0
New Thread - i = 1
New Thread - i = 2
New Thread - i = 3
New Thread - i = 4
New Thread - i = 5
New Thread - i = 6
New Thread - i = 7
New Thread - i = 8
New Thread - i = 9
main Thread - j = 0
main Thread - j = 1
main Thread - j = 2
main Thread - j = 3
main Thread - j = 4
main Thread - j = 5
main Thread - j = 6
main Thread - j = 7
main Thread - j = 8
main Thread - j = 9
Explanation :
- The output of the above program will always be same
- run() method will be executed first
- remaining lines after run() method call in main(args[]) method will be executed
- The reason for the above behavior is because everything will be running under one single Thread i.e.; main Thread
- Note: Multi-threading concept won’t be leveraged if run() method is invoked directly
- So, it is always a better choice to invoke start() method which does few activities and then internally invokes run() method and this way multi-threading concept can be leveraged
5. start() v/s run() :
Difference between start() and run() methods in tabular format,
Sr. No. | start() method | run() method |
1. | start() method present in java.lang.Thread class | run() is abstract method present in java.lang.Runnable interface |
2. | Class extending Thread class shouldn’t override start() method otherwise multi-threading won’t be leveraged | Class implementing Runnable interface must override/implement abstract run() method and provide implementation to leverage multi-threading |
3. | To start a new thread, start() method is invoked which internally invokes run() method after performing mandatory activities | run() method shouldn’t be invoked directly otherwise a separate independent flow of execution won’t be created and this will be executed under main Thread |
Related Articles :
- Java – Introduction to Threads
- Java – Different ways to create/spawn a Thread
- Java – Thread Scheduler
- Java – Important methods and constructors of a Thread class
- Java – Difference between start() and run() methods of Thread class
- Java – Importance of start() method in Thread class
- Java – Overloading run() method of Thread class
- Java – Effects of not overriding run() method of Thread class
- Java – Setting and getting name of a Thread
- Java – Thread priorities
References :
- https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html
Happy Coding !!
Happy Learning !!