重複の可能性:
この同期のポイントは何ですか?
BluetoothChatサンプルアプリケーションを使用してBluetooth接続を確立していますが、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
異なるスレッドから同時に複数回呼び出すことは可能だと思いますが、インスタンスのコピーではなく、メソッドが同期されるのを常に見ています。