Android / Java实现Runnable进入无限循环

时间:2012-09-23 07:40:47

标签: java android multithreading android-intent

我使用以下代码连接两个Android设备P2P。问题是,当服务器从客户端接收数据时,其Thread / Runnable不会停止。

请建议我一种方法来阻止/强迫它进入无限循环。

以下是我用于服务器活动的代码片段:

       public class ServerThread implements Runnable {
            private volatile boolean keepGoing = true;

               public void requestStop() {
                   keepGoing = false;
                }
               public void run() {
                   try {
                       if (SERVERIP != null) {
                           handler.post(new Runnable() {
                               @Override
                               public void run() {
                      serverStatus.setText("Listening on IP: " + SERVERIP);
                    }
                });
                serverSocket = new ServerSocket(SERVERPORT);
                while (true) {
                    // listen for incoming clients
                    Socket client = serverSocket.accept();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            serverStatus.setText("Connected.");
                        }
                    });

                    try {
                        BufferedReader in = new BufferedReader(new            
                             InputStreamReader(client.getInputStream()));
                        line = null;
                        while ((line = in.readLine()) != null) {
                            Log.d("ServerActivity", line);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {

                                    serverStatus.setText(line);


                                    Log.d("IP recevied", line);
                                    requestStop();
                                    //finish();

                                    //Intent i = new Intent(ServerActivity.this, 
                                        //ClientActivity.class);
                                    //startActivity(i);
                                    //serverStatus.setText(line);
                                    // do whatever you want to the front end
                                    // this is where you can be creative
                                }
                            });
                        }

                        break;
                    } catch (Exception e) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                serverStatus.setText("Oops. Connection interrupted. 
                                        Please reconnect your phones.");
                            }
                        });
                        e.printStackTrace();
                    }
                }
            } else {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.setText("Couldn't detect internet connection.");
                    }
                });
                       }
                              } catch (Exception e) {
                                  handler.post(new Runnable() {
                                     @Override
                           public void run() {
                    serverStatus.setText("Error");
                           }
                       });
                       e.printStackTrace();
                   }
               }
           }

编辑:

   while (keepGoing) {
         // listen for incoming clients
         Socket client = serverSocket.accept();
         handler.post(new Runnable() {
             @Override
             public void run() {
                 serverStatus.setText("Connected.");
             }
         });

         try {
             BufferedReader in = new BufferedReader(new            
                  InputStreamReader(client.getInputStream()));
             line = null;
             while (((line = in.readLine()) != null) && keepGoing) {
                 Log.d("ServerActivity", line);
                 handler.post(new Runnable() {
                     @Override
                     public void run() {

                         serverStatus.setText(line);


                         Log.d("IP recevied", line);
                         requestStop();
                         //finish();

                         //Intent i = new Intent(ServerActivity.this, 
                             //ClientActivity.class);
                         //startActivity(i);
                         //serverStatus.setText(line);
                         // do whatever you want to the front end
                         // this is where you can be creative
                     }
                 });
             }

2 个答案:

答案 0 :(得分:3)

我认为问题在于,即使您发信号通知服务器停止,它仍然停留在对serverSocket.accept();的调用中,等待其他客户端连接。你应该做的是关闭stop方法中的套接字:

public void requestStop() {
      keepGoing = false;
      serverSocket.close();
}

强制代码抛出IOException,然后重新检查while条件并退出。

答案 1 :(得分:0)

你为什么评论这一行

  //finish();

建立连接后?

因为一旦连接就永远不会完成,你就会有一个无限循环