2012-02-15 234 views
0

有沒有辦法讓一個線程啓動一個新線程來等待它啓動的線程停止了?我正在考慮使用鎖定,但如果線程崩潰,鎖將永遠不會釋放。Java線程同步

所以當我的程序調用

cTurnCardOvrerConnection thread = new cTurnCardOvrerConnection("thread3", connection, mPlayerList, mPlayersMessages, lBoard); 

將它等到線程結束?

mPlayerList.WaitForAllPlayers(); 
do 
{ 
    do 
    { 
     r=GetClient(); 
     switch(r) 
     { 
      case 0: return; // exitvon a very bad error 
     } 
    } while(r==2); // loop if it was a timeout wait for this thread to terminate.     

    cTurnCardOvrerConnection thread = new cTurnCardOvrerConnection("thread3", connection, mPlayerList, mPlayersMessages, lBoard);    
    if (CheckTimeStamp()) 
     break; 
} while(mPlayerList.AllPlayersFinished()==false); 

回答

5

您可以使用Thread.join()

當然,如果主線程剛剛啓動輔助線程,然後等待它完成,那麼輔助線程真的沒有用處(只需在主線程上工作)。

+0

嗨,起初我想做多線程for – 2012-02-16 00:43:32