This article is reprinted from the WeChat public account "Java Geek Technology", the author is a fan of Yaxue. Please contact the Java Geek Technology public account to reprint this article. Today, a girl in the team asked me why we use the start method instead of the run method when starting a thread. Fortunately, Ah Fen has been studying all the time, otherwise I would have been really stumped by the girl's questions. In multithreading, if you want to start a thread, you must use the thread.start() method instead of the thread.run() method (What, you are not using the thread.start() method? Be good, you are opening it wrong, don't do it next time Do you have any doubts, why do we always call the start() method, why not just call the run() method to start the thread? And if you look at the source code, you will find that in the thread.start() method, the thread.run() method is actually called to execute
The above comment translates: When a thread starts executing, the JVM calls the run method of this thread That is to say, the run method of the thread is called directly by the JVM. In Java, if we want to call the run method directly, it is also possible because the run method is public in Java.
Since the start method finally calls the run method, and the run method itself supports direct calling, why do we usually call the start method instead of the run method in the programs we write? That's because if you call the run method directly, it's not multithreaded. To facilitate explanation, let's look at a small demo:
The above program runs as follows: You will find that the run method of runThread is executed twice Once the run method runs in its own thread, you can see from Run begin another, current thread: Thread-0 that this thread is running in Thread-0 Another time is because our program code directly calls the run method. At this time, the thread runs in the main thread, which can be seen from Run begin another, current thread: main That is to say, if we call the run method directly, the thread does not run in its own thread, but in the current thread. Why do we create multiple threads? Isn't it because we want multiple threads to execute in parallel? For example, I am now thread A, and another thread is started. Then I hope that this thread will run with thread A. If the run method is called directly, it will run in thread A. The goal of creating multiple threads is not achieved, how can this be done, right? So when starting a thread, use the start method instead of the run method. This is actually also explained in the source code: the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method). After the JVM calls the thread's run method, the result is that two threads are running simultaneously:
Can a thread be started twice? The girl understood why threads generally use the start method instead of the run method, because calling it would violate our original intention of using multi-threading The girl asked Ah Fan again, can a thread be started twice? The responsible fan quickly told the little sister that it was not allowed If a thread is started twice, it will throw an IllegalThreadStateException. We can also see this error in the source code:
When a thread starts, it first checks whether the value of threadStatus is 0. If the value is not 0, it means that the state of the thread is not new, and an IllegalThreadStateException is thrown. Ah? I actually want to ask Ah Fen, what other states are there in a thread besides new? Ah Fen wrote an article before, would you like to take a look: The interviewer didn't expect that I could talk about the life cycle of a Java thread for half an hour |
<<: My girlfriend suddenly asked me what DNS is...
>>: To promote user migration to 5G, these tasks need to be done in advance
In the previous article, we learned about the fiv...
Setup and connection issues are notoriously diffi...
In the 21st century, the communication network on...
As cellular technology evolves, mobile bandwidth ...
The development of data generation and data proce...
Domestic operators are making great efforts to la...
1. Introduction to HTTP Protocol 👨🏫 Interviewer:...
5G, big data, artificial intelligence (AI), the I...
[51CTO.com original article] There is no doubt th...
Recently, Miao Wei, Minister of Industry and Info...
[[344451]] This article is reprinted from the WeC...
When front-end developers are debugging locally, ...
BuyVM Las Vegas has currently restocked a large n...
Faced with the ever-changing information security...
RAKsmart is offering flash sales for VPS and clou...