Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在BluetoothChat上以不同步的方式在Bluetooth上进行写入示例_Android_Multithreading_Bluetooth - Fatal编程技术网

Android 在BluetoothChat上以不同步的方式在Bluetooth上进行写入示例

Android 在BluetoothChat上以不同步的方式在Bluetooth上进行写入示例,android,multithreading,bluetooth,Android,Multithreading,Bluetooth,这是我第一次参与基于android的蓝牙应用程序,为了更好地理解它的工作原理,我正在学习BluetoothChat示例。 或多或少,我想我已经理解了一切,但我已经到了一个无法再进一步的地步。 以这种方式写入数据的原因是什么 public void write(byte[] out) { // Create temporary object ConnectedThread r; // Synchronize a copy of the ConnectedThread

这是我第一次参与基于android的蓝牙应用程序,为了更好地理解它的工作原理,我正在学习BluetoothChat示例。
或多或少,我想我已经理解了一切,但我已经到了一个无法再进一步的地步。
以这种方式写入数据的原因是什么

public void write(byte[] out) {
    // Create temporary object
    ConnectedThread r;
    // Synchronize a copy of the ConnectedThread
    synchronized (this) {
        if (mState != STATE_CONNECTED) return;
        r = mConnectedThread;
    }
    // Perform the write unsynchronized
    r.write(out);
}
为什么不直接调用mConnectedThread.write(out)?
这次同步的目标是什么

我理解(显然是错误的)这段代码只在主线程上执行,在这种情况下,我们为什么需要这个互斥体

谢谢大家。

附言:对不起,我的英语很差

对于问题部分:此同步的目标是什么?-目标似乎是以线程安全的方式访问mThread和mConnect变量(即,在访问一个变量时,其他变量没有变化)。此链接(以及其中的其他链接)将通过更多的灯光显示