2016-05-26 37 views
0

我正在開發一個使用Wifi/TCP連接與機器人進行通信的應用程序。目前,我正在使用一個單線程來管理使用TcpClient類和我的MainActivity中的「ConnectTask extends AsyncTask」的tcp連接。在AsyncTask中處理程序的使用

到目前爲止,這是我:

TcpClient.java

private OnMessageReceived mMessageListener = null; 

private int bufferSize = 5000; 
public ByteBuffer bf; 
private BufferedInputStream inFromServer; 
private BufferedOutputStream outFromClient; 
. 
. 
. 
public void run() { 

    mRun = true; 
    try { 
     //here you must put your computer's IP address. 
     InetAddress serverAddr = InetAddress.getByName(SERVER_IP); 

     Log.e("TCP Client", "C: Connecting..."); 

     //create a socket to make the connection with the server 
     Socket socket = new Socket(serverAddr, SERVER_PORT); 

     try { 
      Log.i("Debug", "inside try catch"); 

      //receives the message which the server sends back 

      inFromServer = new BufferedInputStream(socket.getInputStream()); 
      outFromClient = new BufferedOutputStream(socket.getOutputStream()); 


      bf = ByteBuffer.allocate(bufferSize); 




      while (mRun) { 
       // Log.i("Debug", "inside while mRun"); 

       bf.order(ByteOrder.LITTLE_ENDIAN); 
       for (int i=0;i<5000;i++) { 
        int b = inFromServer.read(); 

        if (b == -1) { 
         break; 
        } 

        bf.put((byte) b); 

       } 

       if (bf != null && mMessageListener != null) { 
        //call the method messageReceived from MyActivity class 
        // Log.i("Debug","Message received !"); 
        mMessageListener.messageReceived(bf); 
        mMessageListener.updateBatteryLvl(); 
       } 
       bf.position(0); 
      } 
     } 
. 
. 
. 

這是我在MainActivity ConnectTask:

public class ConnectTask extends AsyncTask<Void, ByteBuffer, TcpClient> { 

    @Override 
    protected TcpClient doInBackground(Void... params) { 
     //we create a TCPClient object and 
     mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() { 
      @Override 
      //here the messageReceived method is implemented 
      public void messageReceived(ByteBuffer message) throws IOException { 
       //this method calls the onProgressUpdate 
       byte[] resultat = new byte[4000]; 
       resultat = message.array(); 
       updateBatteryLvl(); 
       message.clear(); 
我沒有使用任何TcpClient的處理程序

或connecttask,但我已經看到一些使用它們的Tcp連接教程,我想知道他們爲什麼使用它?我已經測試了與機器人的連接,並且收到它發送的數據非常好。 處理程序是否像信號量一樣使用,如果你有多線程寫在同一個文件中例如?

回答

0

如果你有多線程寫在 相同的文件例如處理程序使用像信號量?

或多或少,是的。 處理程序只能使用活套(在主線程中是帶活套的線程)在線程中創建(調用其構造函數)。

然後,您可以發佈Runnables和消息以由處理程序運行(將來某個時間)。這些可運行程序/消息由hanlder排入隊列,並派發到它們創建的線程上運行。所以基本上,如果你在不同的線程中運行東西,並且想要在沒有競爭條件的情況下執行某些操作(或者需要這些操作在特定線程上運行,例如需要更新UI時),則可以發佈「操作「在處理程序中,這就是所有。

對於你的情況,使用AsyncTask,它做同樣的事情,在分離的線程中完成工作,並將結果提交給主線程。如果您需要管理不同的線程,並將結果提交到單個線程,而不是MAIN THREAD,則需要使用處理程序。 但是,當你描述你在做什麼時,這是沒有必要的。

0

您不能在UI線程(主線程)中執行長時間運行操作,否則您將獲得"application not responding"(ANR)。 所以你必須使用多線程和Android框架提供的基礎設施更容易與主線程的數據同步(Android UI工具包不是線程安全的)。

您的選擇可能會導致問題,因爲:

  • 的AsyncTask應該只用於那些需要相當長几秒鐘的操作。

  • AsyncTasks在單個後臺線程(來自API 11)上串行執行。如此長時間的跑步員可以阻止其他人。

  • AsyncTasks不尊重你的主機Activity的生命週期(你可能會得到內存泄漏)。

  • 查殺列表中的優先級爲low

相關問題