What is the difference by calling start() over run() method in java thread

Difference between start()  and run() method in java threads:  the start() and run() method comparison or how they are different is common introductory level multi-threading interview question.  “Why to do one call start method of the thread if start() calls run() ” or “What is the difference by calling start() over run() method in java thread”.  The most common mistake done by a Java programmer is that he tries to implement threads by either overriding the run() method of the Thread class or by implementing the Runnable interface and then calling start() on the thread.

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

What distinguishes the start method from the run method then? The primary distinction is that when a program calls the start() function, a new Thread is generated and the code inside the run() method runs in that Thread, however if you call the run() method directly, no new Thread is formed and the code inside run() runs on the current Thread. The most common mistake done by a The majority of the time, using run() is a bug or programming error because the caller intends to call start() to start a new thread.

Always use the start() method if you want to execute a time-consuming operation; otherwise, your main thread would become stuck while performing a time-consuming task if you use

package core.java.planforexams.basics;

public class planforexams_ThreadStartRun {

	public static void main(String args[]) {
	       
        Thread threadOne_Start = new Thread(new ThreadExecutor("Thread One is started"));
        Thread threadTwo_Run = new Thread(new ThreadExecutor("Thread Two is running"));
       
        threadOne_Start.start(); 
        threadTwo_Run.run();  

    }

    private static class ThreadExecutor implements Runnable{
        private String threadValue;
       
        public ThreadExecutor(String threadValue){
            this.threadValue = threadValue;
            System.out.println("threadValue::"+ threadValue);
        }
       
        @Override
        public void run() {
            System.out.println("Thread Execution: "+ threadValue + " :::: " + Thread.currentThread().getName());
           
        }        
    }
}

Thread Output: 
threadValue::Thread One is started
threadValue::Thread Two is running
Thread Execution: Thread Two is running :::: main
Thread Execution: Thread One is started :::: Thread-0

You cannot invoke the start() function on the thread object more than once, which is another difference between start and run in Java threads. Once begun, calling start() a second time in Java will result in an IllegalStateException, however you can call run() again.

The primary difference between start() and run() method is that the start() method in Thread produces a new thread, whereas the run() method makes no new threads and just operates in the current thread like a regular method call.

Leave a Reply

Your email address will not be published. Required fields are marked *

*