Java无响应的线程

时间:2016-07-03 22:01:03

标签: java multithreading

我正在编写一个TicTacToe游戏程序,我已经创建了所有方法并处于调试阶段,但是我得到了一个无响应的线程问题。在我的Runnable处理程序类中创建的这个线程没有响应:

     public void run(){

    Thread thisThread = Thread.currentThread();
    if(gui.playTwoFirst.isSelected()&&gui.computerPlay.isSelected()){
        myTurn = true;
       }
    while (playing == thisThread){
       if(myTurn == true){

          takeTurn();
       }else{
          //wait for my turn

       }
    }
}

一旦调用runnable方法,线程永远不会识别,“myTurn”布尔值可能变为true,而takeTurn();方法永远不会被调用,因此游戏实际上会停止。 如果我使用这个代码就可以了:

     public void run(){

    Thread thisThread = Thread.currentThread();
    if(gui.playTwoFirst.isSelected()&&gui.computerPlay.isSelected()){
        myTurn = true;
       }
    while (playing == thisThread){
        System.out.println("");
       if(myTurn == true){

          takeTurn();
       }else{
          //wait for my turn

       }
    }
}

唯一的区别是一个小的System.out.println();什么都不做,但由于某种原因允许线程看到myTurn变为真,并允许调用takeTurn()方法。

我做错了什么,或者有更好的方法吗?

0 个答案:

没有答案