Android- Thread.join()导致应用程序挂起

12

我有一个线程来处理游戏循环,当我在这个线程上调用.join()时,应用程序停止响应。 我一直在尝试解决一个问题,即程序永远无法到达代码,也就是线程永远不会终止。

System.out.println("YAY");

游戏循环线程:

该线程成功打印出 "游戏结束",但似乎永远无法完成。

Runnable startGameLoop = new Runnable() {//game loop
          @Override
          public void run() {

              AiFactory ai = new AiFactory();
              final Button play = (Button) findViewById(R.id.Play);
              final Button pass = (Button) findViewById(R.id.Pass);

              while(finish==false){
                  try {
                        Thread.sleep(500);
                  } catch (InterruptedException e) {
                        e.printStackTrace();
                  }
                  currentPlayer = game.getPlayer();

                  if(currentPlayer.isPlayer()==false){

                      //System.out.println("passCount:" +game.getPasscount());
                      ai.decide(nextPlay, game.getPreviousPlayed(), currentPlayer, game.getPasscount() );
                      if(nextPlay.size()!=0){
                          runOnUiThread(new Runnable(){
                            public void run(){
                                changeArrow();
                                if(nextPlay.size() ==1){
                                    play1Card();
                                }
                                else if(nextPlay.size()==2){
                                    play2Card();
                                }
                                else if(nextPlay.size()==3){
                                    play3Card();
                                }
                                else if(nextPlay.size()==5){
                                    play5Card();
                                }
                            }
                      }
                      else{
                          game.addPassCount();
                          game.nextTurn();
                          runOnUiThread(new Runnable(){
                                public void run(){
                                    changeArrow();
                                }
                            });
                      }     
                   }
                   else if (currentPlayer.isPlayer()==true){
                      runOnUiThread(new Runnable(){
                            public void run(){
                                changeArrow();
                                play.setClickable(true);
                                 if(game.getPasscount()==3){
                                     pass.setClickable(false);
                                 }
                                 else{
                                     pass.setClickable(true);
                                 }
                            }
                        });
                  }

             }
             System.out.println("Game Ended");
          }
     };

启动线程并将线程加入到主线程:

     Thread myThread = new Thread(startGameLoop);

     myThread.start();
     try {
        myThread.join();
    } catch (InterruptedException e) {
            // TODO Auto-generated catch block
        e.printStackTrace();
    }

     System.out.println("YAY");
}

据我所知,.join() 方法会让当前线程等待直到新线程执行结束才继续执行。

如果我的问题很愚蠢,对不起,因为我对线程编程很陌生。


2
我们需要更多的上下文信息。join() 方法是在哪里被调用的?很可能是在 UI 线程上执行,这就解释了为什么你的应用程序会挂起。 - Code-Apprentice
在一个Activity类中,join发生在onCreate()方法中。 - Wayne Yun Leung
你的应用程序正在等待一个线程终止,然后才完成创建活动。这 真的 是你想要做的吗?如果不是,那么你在这里想要做什么呢? - Code-Apprentice
啊,这是我的两个错误。我插入了中断来结束我的线程,同时也因为使用join调用阻塞了UI线程。 感谢你们的提醒。 - Wayne Yun Leung
3个回答

7

Thread.join使当前线程等待,直到您调用它的线程结束。 它不会导致您调用它的线程结束。 因此,如果该线程永远不会结束(正如您在顶部所说),调用join将使其永久挂起。

如果您想要实际结束线程,请调用cancel。 但是,这需要您的线程偶尔调用isCanceled()并在其返回true时退出其运行函数。


7
当你在Activity.onCreate中调用myThread.join()时,会阻塞主UI线程。当然,这看起来像你的应用程序已停止响应,因为UI线程负责重新绘制UI。所有你的runOnUiThread调用都不会发生,因为你的UI线程正忙于等待游戏循环。

1

mythread.join()不能保证你的"mythread"已经结束。

你需要在游戏循环中放置一个notify()。

... ...

System.out.println("Game Ended");
synchronized(mythread){
    mythread.notify();
}

基本上,当您调用wait()时,代码会在线程的监视器上等待,而调用notify()会导致等待对象监视器的线程唤醒。 另外,您可以使用notifyAll()来唤醒所有等待的线程。
或者,您可以使用wait()的超时版本,在这种情况下,线程等待直到收到通知或超时。

你最好使用LockCondition来更好地实现这个。 - FindOut_Quran

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接