我正在使用BluetoothChat示例应用程序建立我的蓝牙连接,这就是第218行真的让我烦恼的事情:
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);
}
为什么需要同步ConnectedThread
实例的本地副本,不会同步write
函数(在ConnectedThread
或上面的方法中)。我想可以同时从不同的线程多次调用write
,但我总是看到同步的方法,而不是实例的副本。
答案 0 :(得分:0)
不适用于ConnectedThread
mState
同步应该用于读取和更新。