为什么我们不能直接调用run()方法? [英] Why can't we directly call the run() method?

查看:136
本文介绍了为什么我们不能直接调用run()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果线程的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()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆