In this article, we will discuss different ways to create or spawn a Thread in Java
1. Creating a Thread :
There are 2 ways to create or spawn a Thread, those are –
- By extending Thread class
- By implementing Runnable interface
1.1 Extending a Thread class :
- To create a new Thread, it is very simple to extend Thread class and override run() method providing implementation
MyThread.java
package in.bench.resources.threads;
public class MyThread extends Thread {
@Override
public void run() {
// work for new thread
for(int i=0; i<10; i++) {
System.out.println("value = " + i);
}
}
}
- Above step creates a new Thread but it is yet to be in action as newly created Thread need to be started
- To start a new Thread, start() method of Thread class needs to be invoked after instantiating newly created Thread class
- Now, newly created Thread (child-thread) will execute under main Thread
- Note: There will be always one main Thread under which many child Threads can be created
MainTest.java
package in.bench.resources.threads;
public class MainTest {
// main Thread
public static void main(String[] args) {
// create a new Thread
MyThread t = new MyThread();
// start a new Thread
t.start();
// this will be executed under main Thread
for(int i=0; i<10; i++) {
System.out.println("Main Thread value = " + i);
}
}
}
Output :
Main Thread value = 0
Main Thread value = 1
Main Thread value = 2
Main Thread value = 3
Main Thread value = 4
Main Thread value = 5
Main Thread value = 6
Main Thread value = 7
Main Thread value = 8
Main Thread value = 9
New Thread value = 0
New Thread value = 1
New Thread value = 2
New Thread value = 3
New Thread value = 4
New Thread value = 5
New Thread value = 6
New Thread value = 7
New Thread value = 8
New Thread value = 9
Explanation :
- In the above example, there are 2 Threads,
- one is main Thread which will be always there when program starts/executed
- another Thread is the newly created
- When program is started/executed then main Thread is created under which many new Threads can be created – child Threads
- Now, both Threads will be executed in any fashion or order
- Here, main Thread executed first and then new Thread is executed and this can’t be true every time as it is possible that,
- new Thread can be executed first and then main Thread
- both main & new Threads can be executed parallelly as and when they get their opportunities provided by the Thread Scheduler
- Thread Scheduler allocates/schedule CPU/processor for the waiting Threads
1.2 Implementing Runnable interface :
- Another approach to create a new Thread is to implement Runnable interface and override run() method providing implementation
- The advantage of this approach over the other approach is another class can be extended as per business requirement
- Note: Java allows maximum of only one class can be extended & any number of interfaces can be implemented
MyRunnable.java
package in.bench.resources.threads;
public class MyRunnable implements Runnable {
@Override
public void run() {
// work for new thread
for(int i=0; i<10; i++) {
System.out.println("New Thread value = " + i);
}
}
}
- Again, above step creates a new Thread but it is yet to be in action as newly created Thread need to be started
- To start a new Thread,
- Instantiate newly created class – the one which is implementing Runnable interface
- Instantiate Thread class and pass runnable object created in the above step as constructor–argument
- Invoke start() method of newly created Thread
- Now, newly created Thread (child-thread) will execute under main Thread
- Note: There will be always one main Thread under which many child Threads can be created
MainTest.java
package in.bench.resources.threads;
public class MainTest {
// main Thread
public static void main(String[] args) {
// 1. instantiate Thread/Runnable class
MyRunnable r = new MyRunnable();
// 2. instantiate Thread class and pass runnable as constructor-arg
Thread t = new Thread(r);
// 3. start a new Thread
t.start();
// this will be executed under main Thread
for(int i=0; i<10; i++) {
System.out.println("Main Thread value = " + i);
}
}
}
Output :
New Thread value = 0
New Thread value = 1
New Thread value = 2
New Thread value = 3
New Thread value = 4
New Thread value = 5
New Thread value = 6
New Thread value = 7
New Thread value = 8
New Thread value = 9
Main Thread value = 0
Main Thread value = 1
Main Thread value = 2
Main Thread value = 3
Main Thread value = 4
Main Thread value = 5
Main Thread value = 6
Main Thread value = 7
Main Thread value = 8
Main Thread value = 9
Explanation :
- In the above example, there are 2 Threads,
- one is main Thread which will always be there when program starts/executed
- another Thread is the newly created
- When program is started/executed then main Thread is created under which many new Threads can be created – child Threads
- Now, both Threads will be executed in any fashion or order
- Here, new Thread executed first and then main Thread is executed and this can’t be true every time as it is possible that,
- main Thread can be executed first and then new Thread
- both main & new Threads can be executed parallelly as and when they get their opportunities provided by the Thread Scheduler
- Thread Scheduler allocates/schedule processor for the waiting Threads
2. Thread class v/s Runnable interface :
Both Thread class and Runnable interface can be used to spawn a new Thread by overriding run() method but they’re 2 different approaches
2.1 Thread class :
- Java allows maximum of one class to be extended
- So it becomes difficult to extend another class even when it is required for business purpose
- Also, whenever a new Thread is created using this approach a unique object is created every time
- Thread class implements Runnable interface overriding run() method providing dummy implementation
Thread.java
public class Thread implements Runnable {
// methods and constructors
@Override
public void run() {
if (target != null) {
target.run();
}
}
}
2.2 Runnable interface :
- Creating a new Thread by implementing Runnable interface allows to extend another class if required
- Also, whenever a new Thread is created using this approach it shares the same object for multiple Threads
- Runnable interface has only one abstract method – run()
- Whenever Runnable interface is implemented by any class then abstract run() method need to be overridden providing implementation
Runnable.java
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
In the following article, we will discuss Thread Scheduler in detail
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 !!