Java线程问题处理信息数据被覆盖下一个消息 [英] Java threading issue with handler message data being overwritten by next message

查看:202
本文介绍了Java线程问题处理信息数据被覆盖下一个消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

予具有不同于将数据发送到一个处理程序的主要UIThread蓝牙流线程读取数据,因为它来自于(基于蓝牙聊天样品)。

I have a thread reading data from a bluetooth stream that sends the data to a handler on the main UIThread as it comes in (based on the Bluetooth Chat Sample).

我发现弹出相当频繁穿线的问题。首先,一些code,以供参考。

I've discovered a threading problem that pops up quite frequently. First, some code for reference.

BluetoothService.java (就在那读进来的数据流,它已正确设置该code运行前的部分)。

BluetoothService.java (Just the part that reads the incomming data stream. It has been set up correctly before this code runs).

public void run() {
     DebugLog.i("BluetoothService", "BEGIN mConnectedThread");
     byte[] buffer = new byte[1024];
     int bytes;
         // Keep listening to the InputStream while connected
     while (true) {
          try {
             // Read from the InputStream
             bytes = mmInStream.read(buffer);

             DebugLog.d("BluetoothService", new String(buffer, 0, bytes));
             // Send the obtained bytes to the UI Activity
             mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                          .sendToTarget();
          } catch (IOException e) {
                DebugLog.e(TAG, "disconnected", e);
                connectionLost();
                break;
          }
     }
}

在我的主要活动定义的处理程序(部分):

Handler defined in my main activity (partial):

// The Handler that gets information back from the BluetoothService
private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
          case BluetoothService.MESSAGE_READ:
            byte[] readBuf = (byte[]) msg.obj;
            // construct a string from the valid bytes in the buffer
       String readMessage = new String(readBuf, 0, msg.arg1);
       DebugLog.d("BluetoothHandler", readMessage);
       mConversationArrayAdapter.add(mConnectedDeviceName+":  " + readMessage);
                 break;

        }
    }
};

时的一个小数据,连续流进入该 mmInStream 我的问题就来了。例如,ABCD。如果ABCD被读取一次全部,这code正常功能和日志记载:

My problem comes when a small, continuous stream of data comes into the mmInStream. For example, 'abcd'. If 'abcd' is read all at once, this code functions normally and the log reads:

BluetoothService: BEGIN mConnectedThread
BluetoothService: abcd
BluetoothHandler: abcd

但是,如果数据的连续块被读入2份,第二组数据的由它得到的处理程序的时间覆盖第一组数据。下面是我见过一些例子日志。

But if that continous block of data gets read in 2 parts, the second set of data overwrites the first set of data by the time it gets to the handler. Here's some example logs that I've seen.

BluetoothService: BEGIN mConnectedThread
BluetoothService: a
BluetoothService: bcd
BluetoothHandler: b
BluetoothHandler: bcd

或者

BluetoothService: BEGIN mConnectedThread
BluetoothService: abc
BluetoothService: d
BluetoothHandler: dbc
BluetoothHandler: d

或者

BluetoothService: BEGIN mConnectedThread
BluetoothService: ab
BluetoothService: cde
BluetoothHandler: cd
BluetoothHandler: cde

请注意,获取总是发送第二条消息将覆盖第一条消息的数据只到最短消息的长度。此外,之前的第一条消息已被 mHandler 处理这两个消息总是发送。

Notice that the second message that gets sent always overwrites the first message's data and only up to the shortest message's length. Also, both messages are always sent before the first message has been processed by mHandler.

我猜测我是在某个地方之前的第一个消息被完全处理覆盖掉了一个共同的缓冲区,但我没有看到。有什么建议?

I am guessing that I'm a common buffer somewhere is getting overwritten before the the first message is fully processed, but I don't see where. Any suggestions?

推荐答案

有没有可能是你在创建消息对象时使用第二个字节的缓冲区?

Is it possible that you have to use a second byte buffer when creating the message object?

mHandler.obtainMessage(MESSAGE_READ, bytes, -1, copyOfBuffer)

我有一个怀疑,mHandler(虽然我不知道它是什么),是保持一个对字节数组,你送他。

I have a suspicion that mHandler (although I don't know what it is) is keeping a reference to that byte array you send him.

这篇关于Java线程问题处理信息数据被覆盖下一个消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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