<分区>
什么需要通过start方法来运行线程?为什么我们不直接调用运行方法?
What will happened if combined the code of start() and run()
to make it as single method run()
不解释这两种方法之间的区别,我想知道这种情况。
<分区>
什么需要通过start方法来运行线程?为什么我们不直接调用运行方法?
What will happened if combined the code of start() and run()
to make it as single method run()
不解释这两种方法之间的区别,我想知道这种情况。
最佳答案
当程序调用 start() 方法时,会创建一个新线程并在新线程中执行 run() 方法中的代码,而如果直接调用 run() 方法,则不会创建新线程并在 run() 中执行代码将在当前线程上执行。
每个线程都在一个单独的调用堆栈中启动。
从主线程调用 run() 方法,run() 方法进入当前调用堆栈,而不是新调用堆栈的开头。
请引用What if we call run() method directly instead start() method?另请阅读 Difference between start and run method in Thread
关于java - 线程启动方法需要什么?为什么不只有运行方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24843040/