Ted pottel
Ted pottel

Reputation: 6993

Java Thread synchronization

Is there a way for a thread that starts a new thread to wait until the thread it started has stopped? I was thinking about using locked, but then if the thread crashes, the lock will never get release.

so when my program calls

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

will it wait until the thread is finished?

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);

Upvotes: 0

Views: 126

Answers (2)

Try using a CountDownLatch.

Upvotes: 0

jtahlborn
jtahlborn

Reputation: 53694

you can just use Thread.join().

of course, if the primary thread is just launching the secondary thread and then waiting for it to finish, there's really no use for the secondary thread (just do the work on the primary thread).

Upvotes: 5

Related Questions