这里的同步点是什么?这种同步的重点是什么?
为什么不只是使用mConnectedThread.write(out)
?
的代码片段是从BluetoothChat样品为Android (found here)
需要/**
* Write to the ConnectedThread in an unsynchronized manner
* @param out The bytes to write
* @see ConnectedThread#write(byte[])
*/
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);
}
当然,不知道更多的连接仍然可以在同步块的结束和实际的write()调用之间关闭......现在可能会产生完全不同的错误。而且,如果ConnectThread在正常取消后处理write(),那么简单地将它分配给一个局部变量并检查null将是一种更简单的方法。 – PSpeed 2009-12-30 23:04:54