我在這裏閱讀了有關Swing併發的知識Concurrency in Swing但我不明白在哪裏執行我的工作線程在我的項目中 我有一個面板描繪了它的組件,如果有東西被點擊。面板類似乎是這樣的。點擊鼠標後,我們調用board方法callAI();我應該在哪裏放置工作線程
public class BoardPanel extend JPanel implements MouseListener , MouseMotionListener
{
private Board myBoard;
public BoardPanel()
{
//////////// Some code here
}
public void paintComponent(Graphics g)
{
///Some code here
}
public void mouseClicked(MouseEvent event)
{
///Some changes in the panel components.
repaint();
myBoard.callAI();
}
}
板類實現如下。在callAI()方法中,調用類AIPlayer的對象播放器的方法startThinking()。這是消耗CPU並在方法startThinking()完成後使面板凍結並重新繪製的方法。
public class Board
{
private AIPlayer player;
public Board()
{
//some code here.
}
public void callAI()
{
player.startThinking();
}
}
正如我從我讀到的理解我需要使用工作線程。但我不明白我應該在哪個班級使用它。我認爲它與Swing組件有關,所以我需要在BoardPanel類中使用它,但是我不能擴展SwingWorker類,因爲我已經擴展了JPanel。我應該使用BoardPanel類中的任何匿名類嗎? 所以我的問題是,我應該在哪裏擴展SwingWorker類並使用doInBackground()方法以及爲什麼。
是的,這是正確的方向,謝謝 –