如何在主程序中等待所有生成的线程结束?
我在主程序中,interpreter是个实现了Runnable接口的对象组,在这个循环中生成很多线程,并start
while(i<intColumnsCount){
interpreter[i]=new DatabaseInterpreter() ;
//tg为线程组
Thread th = new Thread(tg,interpreter[i]);
th.start() ;
i++ ;
}
由于我要得到各个线程(它们都进行运算,并各生成一个值)执行完后的各个值,所以我得等待它们全部执行完,所以:
while( tg.activeCount()>0 )
{
try
{
Thread.currentThread().sleep(1000);
}
catch(InterruptedException e){}
}
这样可以吗?其中Thread.currentThread().sleep(1000)我的本意是主线程休息,是不是这样呢?