JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Difference between Thread.start() and Thread.run() method in Java?

A thread is started in Java by calling the start() method of java.lang.Thread class, but if you learn more you will find out that start() method internally calls the run() method of Runnable interface to execute the code specified in the run() method in a separate thread. Now the question comes, why can't you just call the run() method instead of calling the start() method? Since start() is calling the run() anyway, calling run directly should have the same effect as calling the start, no?


when you directly call the run() method then the code inside run() method is executed in the same thread which calls the run method. JVM will not create a new thread until you call the start method.

when you call the Thread.start() method, then the code inside run() method will be executed on a new thread, which is actually created by the start() method

Another key difference between start and run method to remember is that you can call the run method multiple time, JVM will not throw any error but when you cannot call the start() method on same thread instance.


The first time, t.start() will create a new thread but the second time it will throw java.lang.IllegalStateException, because the thread is already started and you cannot restart it again, you can only pause a thread in Java. Once it died it's gone.


Difference between start() and run() method of Thread class
The run() method comes from the Runnable interface but the start() method is only declared in the Thread class. Since java.lang.Thread class implements Runnable interface, you can access the run() method from an instance of Thread class.

I have created following Java program to demonstrate to you the difference of calling start() method on the thread and calling the run() method directly from the main thread.

Remember, in both the cases the run() method will be called but when you call the start() method then it will be called by a new thread.

On the other hand, if you call the run() method directly than it will be called on the same thread i.e. main thread in our case.

Don't believe? run the code and see by yourself.  Let's see the example now.

public class HelloWorldApp {

    public static void main(String args[]) {
        Thread t = new Thread() {

            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()
                        + " is executed the run() method");
            }
        };

        System.out.println( Thread.currentThread().getName() + " Calling the start() method of Thread");
        t.start();
        
        // let's wait until the thread completes execution
        try {
            t.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        System.out.println( Thread.currentThread().getName() + " Calling the run() method of Thread");
        t.run();
    }
}

output:

main Calling the start() method of Thread
Thread-0 is executed the run() method
main Calling the run() method of Thread
main is executed the run() method

Conclusion 

difference between start() and run() method in Java. Just remember that even though start() method internally calls the run() method, its main purpose is to create a new thread. If you directly call the run() method than a new thread will not be created instead run() will get executed on the same thread. It means you should always start the thread by calling Thread.start() method in Java.

.