Threads in Java

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

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

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()

Similar Java Tutorials