在多线程中,如果想让一个线程启动,你使用的方法一定是 thread.start()
方法,而不是 thread.run()
方法(啥,你用的不是 thread.start()
方法?你的打开方式不对哦,下次不要这样了~
有没有疑惑,为什么每次我们都习惯调用 start()
方法,为什么不直接调用 run()
方法来启动线程呢?
而且如果去看源码的话,你会发现,在 thread.start()
方法中,其实最后还是调用了 thread.run()
方法来执行
Causes this thread to begin execution; the Java Virtual Machine
calls the <code>run</code> method of this thread.
上面的注释翻译一下:当线程开始执行时, JVM 会调用此线程的 run 方法
也就是说,线程的 run 方法是由 JVM 直接调用的,在 Java 中如果我们想要直接调用 run 方法也是可以的,因为在 Java 中 run 方法是 public 的
@Override
public void run() {
if (target != null) {
target.run();
}
}
那既然 start 方法最后调用的也是 run 方法,再加上 run 方法本身支持直接调用,那为啥我们平时写的程序都是调用 start 方法,而不是 run 方法呢
那是因为,如果直接调用 run 方法,就不是多线程了
为了方便解释,咱们看个小 demo :
public class RunThread {
public static void main(String[] args) {
Thread runThread = new Thread(new Runnable() {
@Override
public void run() {
System.out.printf("Run begin another , 当前线程 : %s.%n" ,Thread.currentThread().getName());
}
});
// 启动线程
runThread.start();
// 直接调用 run 方法 -- 演示使用,实际中不要这么做!
runThread.run();
System.out.printf("Run begin , 当前线程 : %s.%n" ,Thread.currentThread().getName());
}
}
上面的程序运行结果如下:
你会发现, runThread
的 run
方法被执行了两次
一次是 run 方法运行在自己的线程中,从 Run begin another , 当前线程 : Thread-0
可以看到,这个线程是运行在 Thread-0
中
另外一次是因为我们的程序代码直接调用了 run 方法,此时的线程运行在 main 线程中,从 Run begin another , 当前线程 : main
可以看出来
也就是说,如果我们直接调用 run 方法的话,线程并不是运行在自己的线程中,而是运行在了当前线程中
我们为什么要创建多线程?不就是希望多个线程并行执行,比如现在我是线程 A ,此时又起了一个线程,那么我希望这个线程是和线程 A 一起运行的,如果直接调用了 run 方法的话,就运行在线程 A 里面来了
并没有达到创建多线程的目标,这怎么行呢,对不对
所以在启动线程时,都是使用 start 方法,而不是 run 方法
这一点,其实在源码中也有说明:
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).
在 JVM 调用线程的 run 方法之后,结果就是两个线程同时运行:
- 当前线程(从调用返回到 start 方法)
- 另一个线程(执行 run 方法)
以上,感谢您的阅读~