Difference between start and run methods in java multithreading


This article mainly introduces the relevant information about the difference between the start method and the run method in the java thread. What is the difference between calling the start method and the run method in the java thread? These two questions are two very popular beginner-level multi-threaded interview questions, which are explained in detail here. Friends who need them can refer to:

The difference between the start method and the run method in a thread

In a thread, if the start method calls the run method in turn, why do we choose to call the start method? Or what is the difference between calling start method and run method in java thread? These two questions are two very popular beginner level multithreaded interview questions. When a Java programmer begins to learn threads, they first learn to inherit from 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, by looking at the API documentation or other means, he will find that the run method is called inside the start method, but many of us only realize the importance of this question when we are asked in the interview. In this java tutorial, we will understand the difference between calling start method and run method when starting a thread in java

This post is a post-procedure to some of our posts on Java Multithreading, EG Difference between Runnable and Thread in Java AND How to solve Producer Consumer problem in Java using BlockingQueue. If you haven't read them, you probably will find them still interesting and useful

The difference between start and run in java thread

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

The following is the demo of the start method and the run method

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

Here you can clearly see the difference between the two

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 it can help you, thank you for your support to this site!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325899187&siteId=291194637