我有一个处理我的游戏循环的线程,当我在这个线程上调用.join()时应用程序停止响应.
我一直在努力解决程序永远无法访问代码的问题,因为线程永远不会结束.
我一直在努力解决程序永远无法访问代码的问题,因为线程永远不会结束.
System.out.println("YAY");
我的游戏循环线程:
这个帖子成功打印出“Game Ended”但似乎永远不会完成.
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()使当前线程等到新线程完成后再继续.
对不起,如果这是一个愚蠢的问题,我对线程很新.