我有时间问题而且我不知道如何修复它。
在我的第二个循环显示卡中,它循环并且每个连接都启动一个名为cLookAtCardConnection
的类。这个类有时会设置标志mPlayerList.GetNextFlag()
,从而导致循环退出,程序进入第一个循环,将卡转过来,调用类cLookAtCardConnection
问题是在设置mPlayerList.GetNextFlag()
之后,接下来的几个连接仍然在同一个循环中并且在标志设置和运行时调用类cLookAtCardConnection
而不是(退出?)循环进入调用cLookAtCardConnection
的循环。
为什么在设置标志后退出循环会有延迟?
while( lBoard.Next()==true)
{
mPlayerList.Next();
// inner loop wait for all players
/////////////////////////////////////////////////////////////////////////////
// turn one card over
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
cTurnCardOvrerConnection thread = new cLookAtCardConnection("thread3", connection, mPlayerList, mPlayersMessages, lBoard);
} while( mPlayerList.AllPlayersFinished()==false);// end while
//////////////////////////////////////////////////////////////////////////
// Display card -LOOK AT CARD
mPlayerList.ClearNextFlag();
mPlayerList.WaitForAllPlayers();
do
{
System.out.println(Thread.currentThread()+": Display card \r");
do{
r=GetClient();
switch(r)
{
case 0: return; // exitvon a very bad error
}
} while(r==2);// loop if it was a timeout
cLookAtCardConnection thread = new cLookAtCardConnection("thread3", connection, mPlayerList, mPlayersMessages, lBoard);
// after this flag is set the next couple connectons are still in this loop???
} while( mPlayerList.GetNextFlag()==false);// end while
} // reloop game board
} // loop forever
// System.out.println("--------- Eit player loop ------------------- \r");
} catch(IOException ec)
{
System.out.println(ec.getMessage());
}
} // end run
} // end of class
答案 0 :(得分:2)
您可能需要设置synchronized
块,以便do-while
循环内的代码一次只能访问一个线程。