The differences between the start and run methods in the java thread are described in detail

  • 2020-05-19 04:48:47
  • OfStack

The difference between the start and run methods in threads

If the start method calls the run method in sequence in a thread, why do we choose to call the start method? Or what's the difference between calling the start method in an java thread and calling the run method in an java thread? These two questions are two very popular beginner level multithreaded interview questions. When an Java programmer starts learning threads, they first learn to inherit the Thread class, override the run method, or implement the Runnable interface, implement the run method, and then call the start method of the Thread instance. But when he has some experience, he can look at the API documentation or some other way and see that the run method is called from within the start method, but many of us don't realize the importance of this question until we are asked in an interview. In this java tutorial, we will see the difference between calling the start method and the run method when starting threads in java

This post is the preface to one of our new posts on Java multithreading, E.G. Difference between Thread Java How How Producer problem in using BlockingQueue. If you haven't read them yet, You may find them interesting and useful

start differs from run in the java thread

The main difference between start and run methods is that when the program calls start method, 1 new thread will be created, and the code in run method will run on the new thread. However, when you call run method directly, the program will not create a new thread, and the code inside run method will run on the current thread. In most cases, the method of calling run is 1 bug or becomes an error. Because the original intent of the caller is call start method to open a new thread, this error can be detected by many static code coverage tools, such as fingbugs. If you want to run to spend a lot of time, you'd better use start method, otherwise when you call run method, your main thread will be stuck. Another difference is that if 1 thread is started, you cannot call the start method of the thread object repeatedly. Calling the start method of the already started thread will report an IllegalStateException exception, while you can call the run method repeatedly

The following are demo for the start method and demo for the run method

The task in the thread is to print the String value passed in by the thread as well as the name of the current thread

The difference can be seen clearly here




public class DiffBewteenStartAndRun { 
 
 
  public static void main(String args[]) { 
 
 
    System.out.println(Thread.currentThread().getName()); 
    // creating two threads for start and run method call 
    Thread startThread = new Thread(new Task("start")); 
    Thread runThread = new Thread(new Task("run")); 
 
 
    startThread.start(); // calling start method of Thread - will execute in 
                // new Thread 
    runThread.run(); // calling run method of Thread - will execute in 
              // current Thread 
 
 
  } 
 
 
  /* 
   * Simple Runnable implementation 
   */ 
  private static class Task implements Runnable { 
    private String caller; 
 
 
    public Task(String caller) { 
      this.caller = caller; 
    } 
 
 
    @Override 
    public void run() { 
      System.out.println("Caller: " + caller 
          + " and code on this Thread is executed by : " 
          + Thread.currentThread().getName()); 
 
 
    } 
  } 
} 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: