问题描述
如果线程的start()方法内部调用了run()方法,那么为什么不直接在代码中直接调用run()方法呢?这样做涉及哪些问题?
If the start() method of a thread internally calls the run() method, then why don't we directly call the run() method in our code? What are the issues involved in doing so?
推荐答案
start
方法可确保代码在新的线程上下文中运行.如果直接调用run
,则它就像是普通方法调用,它将在 current 线程的上下文中运行,而不是在新线程的上下文中运行. start
方法包含触发新线程的特殊代码. run
显然不具备此功能,因为您在编写run
方法时未包含该功能.
The start
method makes sure the code runs in a new thread context. If you called run
directly, then it would be like an ordinary method call and it would run in the context of the current thread instead of the new one. The start
method contains the special code to trigger the new thread; run
obviously doesn't have that ability because you didn't include it when you wrote the run
method.
这篇关于为什么我们不能直接调用run()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!