Android- Thread.join()导致应用程序挂起 [英] Android- Thread.join() causes Application to hang

查看:172
本文介绍了Android- Thread.join()导致应用程序挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理游戏循环的线程,当我在此线程上调用.join()时,应用程序停止响应. 我一直在尝试解决程序永远无法到达代码的问题,即线程永无休止.

I have a thread which handles my game loop, when i call .join() on this thread the application stops responding. I've been trying to fix a problem where the programs never get to the code, I.E the thread never ends.

System.out.println("YAY");

我的游戏循环线程:

此线程成功打印出游戏已结束",但似乎从未完成.

This thread successfully prints out "Game Ended" but never seems to finish.

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");
          }
     };

启动线程并将其连接到主线程:

Starting and joining the thread to the main thread:

     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()使当前线程等待新线程完成后再继续.

To my understanding .join() makes the current thread wait till the new thread has finished before carrying on.

很抱歉,如果这是一个愚蠢的问题,我对线程技术还很陌生.

Sorry if this is a stupid question, i'm quite new to threading.

推荐答案

Activity.onCreate中调用myThread.join()时,您将阻塞主UI线程.当然,您的应用程序似乎已停止响应,因为UI线程是负责重绘UI的线程.您的所有呼叫runOnUiThread都不会发生,因为您的UI线程正忙于等待游戏循环.

When you call myThread.join() inside Activity.onCreate you block the main UI thread. Of course it looks like your application has stopped responding because the UI thread is the one responsible for redrawing UI. All your calls runOnUiThread never happen because your UI thread is busy waiting on your game loop.

这篇关于Android- Thread.join()导致应用程序挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆