1
我有一個計時問題,我不知道如何解決它。我的紙牌遊戲服務器中的Java線程計時問題
在我的第二個循環顯示卡中,它循環,每個連接開始一個名爲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
重新命名爲'cLookAtCardConnection'的類以及諸如'WaitForAllPlayers()'等方法。 1)請學習常用的Java術語並使用它。 2)一個名爲'cLookAtCardConnection'的類有一個不好的設計氣味。類名應該是名詞(專有名詞),而不是動詞(做'看'等詞)。 – 2012-01-29 07:21:09
另外,也許你應該考慮改進代碼中的評論 - 「退出一個非常糟糕的錯誤」似乎不是一個有用的評論 – Dawood 2012-01-29 07:23:54