首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在所有函数都执行完后暂停main()?

如何在所有函数都执行完后暂停main()?

提问于 2018-01-29 00:13:23
回答 2关注 0查看 512

如何使方法中的最后一行是对System.out.println()的调用?

下面是我代码的相关部分:

代码语言:txt
复制
public static void main(String[] args) {

//some other initialization code

//Make array of Thread objects
Thread[] racecars = new Thread[numberOfRaceCars];

//Fill array with RaceCar objects
for(int i=0; i<numberOfRaceCars; i++) {
    racecars[i] = new RaceCar(laps, args[i]);
}

//Call start() on each Thread
for(int i=0; i<numberOfRaceCars; i++) {
    racecars[i].start();
    try {
        racecars[i].join(); //This is where I tried to using join()
                            //It just blocks all other threads until the current                            
                            //thread finishes.
    } catch(InterruptedException e) {
        e.printStackTrace();
    }
}

//This is the line I want to execute after all other Threads have finished
System.out.println("It's Over!");

}
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档