【发布时间】:2012-09-05 18:08:48
【问题描述】:
我正在使用 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,但我总是看到同步的方法,而不是实例的副本。
【问题讨论】:
标签: java android synchronization