The difference between start method and run method in 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 the start method and the run method in a java thread? These two questions are two very popular multi-threaded interview questions at the beginner level. When a Java programmer starts to learn threads, they will first learn to inherit the Thread class, overload the run method or implement the Runnable interface, implement the run method, and then call the start method of the Thread instance. But after he has some experience, he will find that the start method will call the run method internally by looking at the API documentation or other ways, but many of us know that when we are asked during the interview, we will realize the importance of this question. In this java tutorial, we will understand the difference between calling the start method and the run method when starting threads in java
This post is a postscript to some of our articles published 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 yet, you may find them interesting and useful.
The difference between start and run in java thread
The main difference between 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. However, 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 fingbugs. If you want to run a task that takes a lot of time, it is best to 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 repeatedly call the start method of the thread object. Calling the start method of the thread that has already started will report an IllegalStateException exception, but you can repeatedly call the run method
The following is the demo of the start method and the run method
The task in the thread is to print the name of the current thread passed in the String value.
Here we can clearly see the difference between the two
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 for this site!