01-Java what is a thread and ways to create threads.
what is thread ?
A concurrent smallest unit of execution.
A lightweight process that can be managed independently by the Java Virtual Machine (JVM).
A thread as a separate flow of execution within a program.Threads allow you to run multiple parts of your program concurrently.
Two ways to create threads.
- Extending the Thread class and overriding the run() method.
- By implementing the Runnable interface and implementing the run() method.
Let’s look at the differences of these two.
01-Now let’s look at the coding example of Extending the Thread class and overriding the run() method.
// Use extend keyword to extend Thread class.
public class ExampleExtendThreadClass extends Thread{
// Then overwrite the run method in Thread class.
@Override
public void run(){
System.out.println("I am the overwritten run method of the Thread class current thread name is--"+Thread.currentThread().getName());
for(int i=1;i<=5;i++){
System.out.println(i);
}
}
}
Let’s create two threads using this “ExampleExtendThreadClass” and run them.
public class MainExampleExtendThreadClass {
public static void main(String[] args) {
ExampleExtendThreadClass t1=new ExampleExtendThreadClass();
// use the start, so it's run like a new thread.
t1.setName("T1");
t1.start();
ExampleExtendThreadClass t2=new ExampleExtendThreadClass();
t2.setName("T2");
t2.start();
}
How to start a thread ?
Once you have created a new thread, you can start it by calling the start() method.
This will cause the JVM to create a new thread and execute the run() method concurrently with the other threads in the program.
Why can’t we directly call the run() method to start a thread in java?
The start method contains the special code to trigger the new thread.
The start method makes sure the code runs in a new thread context.
If you called run() directly, then it would be like an ordinary method call,
and it would run in the context of the current thread instead of the new one
02-Now let’s look at the coding example of implementing the Runnable interface and implementing the run() method.
public class CreateThreadByImplementingRunnableInterface implements Runnable{
// use the implement , because then you can extend another class. Or else you can't extend another class.
@Override
public void run() {
System.out.println("I am the implemented run method of the Thread class current thread name is--"+Thread.currentThread().getName());
for(int i=1;i<=5;i++){
System.out.println(i);
}
}
Then create two threads by passing the same instance of the class (CreateThreadByImplementingRunnableInterface) which implemented the runnable interface.
/*
The second way is create an object called "runnable" of the class that implemented the Runnable interface.
Then create a Thread class object and pass the object of runnable.
*/
public static void main(String[] args) throws InterruptedException {
CreateThreadByImplementingRunnableInterface runnable=new CreateThreadByImplementingRunnableInterface();
// Unlike in extend, we can't directly create thread objects.
// Also, we can't call start method directly because we didn't extend the thread class in CreateThreadByImplementingRunnableInterface
// So create a new thread and pass the same runnable object to it.
Thread t1 = new Thread(runnable);
t1.setName("T1");
t1.start();
Thread t2 = new Thread(runnable);
t2.setName("T2");
t2.start();
// then wait till both threads are dead.
t1.join();
t2.join();
System.out.println("What remains should be the main thread, name is-"+Thread.currentThread().getName());
}
Also Runnable interface has only one abstract void method called run().So it’s a funtional interface so rather than using another class to implement it ,using lambda expression we can implement that.
package com.create.thradsways.runnablewithlambda;
public class Main {
public static void main(String[] args) throws InterruptedException {
Runnable runnable = () -> {
System.out.println("I am the implemented run method of the Thread class current thread name is--" + Thread.currentThread().getName());
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
};
// So create a new thread and pass the same runnable object to it.
Thread t1 = new Thread(runnable);
t1.setName("T1");
t1.start();
Thread t2 = new Thread(runnable);
t2.setName("T2");
t2.start();
// then wait till both threads are dead.
t1.join();
t2.join();
System.out.println("What remains should be the main thread, name is-"+Thread.currentThread().getName());
}
}