0

I need a solution from this lines of code i want to perform a 10 loops and at 10 i want it to system.exit(); where my GUI just exit itself, need help! currently this lines would just exit and it doesnt perform anything

int counter =  0;
            while(true){
                counter++;
            for (int i = 0; i < 5; i++) {
                if(counter < 10){
                    new Thread(new Person(bathroom, !isMale)).start();
                    new Thread(new Person(bathroom, isMale)).start();
                }
                else{
                    System.exit(0);
                }

            }
4

2 回答 2

3
System.exit(0);

System.exit将退出所有线程,包括您已启动的线程。

您应该等待所有线程停止后再退出。根据它们的组织方式,我会计算出要启动的线程数,并让它们都等待倒计时锁存器,主线程结束,或者简单但更hacky,让主线程包括:

while(true){

}

在最后。

于 2013-10-11T23:32:22.020 回答
0

在您开始新线程后,它们将采取自己的课程..

您可以使用 join() 函数来确保它们完成运行..

于 2013-10-12T02:32:42.847 回答