The output looks as follows:
Sleeping for 500 seconds within run method
Sleeping for 500 seconds within run method
Within main method
Sleeping for 500 seconds within run method
Within main method
Within main method
Sleeping for 500 seconds within run method
Sleeping for 500 seconds within run method
Within main method
Within main method
Within main method
我注意到在执行run out内Sys方法时,同时运行main方法内的Sys out。
{{1}}
当我们调用thr.start()时,不应该在run()处完成执行,然后返回到while(instance.count!= 5){??
答案 0 :(得分:4)
您的run
方法将不从主线程中调用。相反,start
创建一个新的执行线程(独立于主线程),并且该新线程执行run
。主线程将立即(无需等待新线程发生什么情况)在start
之后恢复下一条指令。
这种并发性正是您要使用Thread
的原因。