问题描述
我正在阅读有关线程的内容,发现我们无法在同一个线程实例上调用两次start方法。但我不明白同样的确切原因。那么为什么我们不能称之为两次甚至更多次?
I was reading about threads and found that we can't call the start method twice on the same thread instance. But I didn't understand the exact reason for the same. So why can't we call it twice or even more times?
推荐答案
在我看来,Thread对象就是你的句柄对于实际运行的上下文。如果你允许创建与同一个java.lang.Thread相关的许多并发执行,那么你期望getStackTrace()和getState()方法返回什么?
In my opinion the Thread object is your "handle" for the actual running context. If you allow creating many concurrent executions associated with the same java.lang.Thread, what would you expect getStackTrace() and getState() methods to return?
我想这是线程类可以设计为允许产生多个运行的上下文,但它的API不那么简单和干净。
I suppose that Thread class could have been designed to allow spawning multiple running contexts, but its API would be less simple and clean.
这篇关于为什么我们不能在Thread对象的同一个实例上调用start方法两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!