這裏的同步點是什麼?這種同步的重點是什麼?
爲什麼不只是使用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