答案 0 :(得分:0)
可视化可能有点困难,但是你的代码与单个线程完全相同,它与你旋转然后加入的两个线程做同样的工作:
thread1.start() // this starts the other thread to do some work
// this is where your main thread would normally do other work
thread1.join() // here your main thread waits for thread1 to complete
thread2.start() // again you start a third thread
// again you do no work here
thread2.join() // but instead immediately wait for the thread to complete
这类似于接力比赛,其中第一个线程暂时移至thread1
,等待她返回"指挥棒"执行,接收接力棒并立即将其交给thread2
并等待她返回指挥棒。"
如果您确实需要3个主题来完成程序的工作,那么您需要这样的内容:
thread1.start()
thread2.start()
def sum = (0..1000).sumBy { it } // or some other work for your main thread to do
thread1.join()
thread2.join()
希望这有帮助。