My sister asked me why I used Start instead of Run when starting a thread.

My sister asked me why I used Start instead of Run when starting a thread.

[[357639]]

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

  1. Causes this thread to   begin execution; the Java Virtual Machine
  2. calls the <code>run</code> method of this thread.

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.

  1. @Override
  2. public void run() {
  3. if (target != null ) {
  4. target.run();
  5. }
  6. }

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:

  1. public class RunThread {
  2. public   static void main(String[] args) {
  3. Thread runThread = new Thread(new Runnable() {
  4. @Override
  5. public void run() {
  6. System. out .printf( "Run begin another , current thread : %s.%n" ,Thread.currentThread().getName());
  7. }
  8. });
  9.  
  10. // Start the thread
  11. runThread.start();
  12.  
  13. // Call the run method directly -- for demonstration purposes only, don't do this in practice!  
  14. runThread.run();
  15.  
  16. System. out .printf( "Run begin , current thread : %s.%n" ,Thread.currentThread().getName());
  17. }
  18. }

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:

  • The current thread (returned from the call to the start method)
  • Another thread (executes the run method)

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:

  1. if (threadStatus != 0)
  2. throw new IllegalThreadStateException();

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

Recommend

How Industry 4.0 and 5G will change supply chain visibility

As the pandemic highlights the serious inefficien...

In fact, IPv6 is not so perfect

Everything has its two sides, and technology is n...

Mercury enables remote procedure calls (RPC) for high performance computing

summary Remote Procedure Call (RPC) is a widely u...

11 key visualizations for SD-WAN success

SD-WAN deployments are quickly becoming a major f...

What is the principle of WebSocket? Why can it achieve persistent connection?

[[396397]] To better understand WebSocket, we nee...

Learn about routers, switches, and network hardware

Today we're taking a look at home network har...

Many manufacturers are competing to lay out the Wi-Fi 6 industry chain

Recently, manufacturers such as Samsung, Huawei, ...

China Mobile: 5G package customers reach 331 million

China Mobile released its unaudited financial dat...

How 5G can unlock the potential of smart homes

5G technology has been one of the hottest topics ...