简体   繁体   中英

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

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

尝试使用CountDownLatch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM