In Java, a thread represents an independent path of execution within a program. Threads in Java allow concurrent execution of multiple tasks.
Defining a Thread in Java
We can define Thread in Java by using the following two ways-
- By extending Thread class.
- By implementing Runnable interface.
Defining Thread by extending Thread class
public class ThreadsExample extends Thread {
@Override
public void run() {
System.out.println("Thread is running");
}
}
class MainThread {
public static void main(String[] args) {
ThreadsExample Object = new ThreadsExample();
Object.start();
}
}
![Threads in Java](https://thecodedata.com/wp-content/uploads/2023/06/image-36.png)
Thread Scheduler
The thread scheduler is a part of JVM and responsible for scheduling threads i.e., if multiple threads are waiting to get a chance for execution then the thread scheduler will decide in which order threads will be executed.
There is no particular Algorithm followed by a thread scheduler, It is varied from JVM to JVM hence we can not expect a particular order of thread execution and exact output.
Difference between start() and run()
In the case of start() a new thread will be created which is responsible for the execution of run() in the thread but in the case of run() no new thread will be created and the run() method is executed like a normal method.
Importance of Thread class start() method
start() is responsible for registering a thread with thread scheduler and all other mandatory activities, hence without executing Thread class start() we can not start a new thread in Java due to this Thread class start() is considered as art of Multi-Threading.
start()
- Register thread with thread scheduler.
- Perform all other mandatory activities.
- Invoke run() method.
Overloading of run() method
Overloading of Thread class run() is possible in Java but Thread class start() invokes no argument run(), For invoking overloaded run() we have to call explicitly
public class ThreadsExample extends Thread {
@Override
public void run() {
System.out.println("No-argument run method");
}
public void run (int i){
System.out.println("Overloaded run method");
}
}
class MainThread {
public static void main(String[] args) {
ThreadsExample object = new ThreadsExample();
object.start();
object.run();
object.run(7);
}
}
![Overloading of run() method](https://thecodedata.com/wp-content/uploads/2023/06/image-37.png)
Overriding of run() method
If we are not overriding run() then Thread class run() will be executed which has an empty implementation.
Overriding of start()
If we override the Thread class start() then the start() method will execute like a normal method and no new thread will be created.
public class ThreadsExample extends Thread {
@Override
public synchronized void start() {
System.out.println("Overriding start");
}
}
class MainThread {
public static void main(String[] args) {
ThreadsExample object = new ThreadsExample();
object.start();
}
}
![Overriding of start()](https://thecodedata.com/wp-content/uploads/2023/06/image-38.png)
Similar Java Tutorials
- Odd Even in Java
- Method to print exception information in Java
- Exception Handling by Using try-catch in Java
- Checked Exception vs Unchecked Exception
- Exception Hierarchy in Java
- Java Exception Handling Interview Questions
- final finally finalize in java
- User Defined Exception in Java
- Exception Handling Keyword in Java
- throw throws in Java
- try with multiple catch block in Java
18 thoughts on “Threads in Java”