Possible Duplicate:
What's the point of this synchronization?
I’m using the BluetoothChat sample application to make my Bluetooth connection and there’s this thing that’s really bugging me at line 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);
}
Why would one need to synchronize a local copy of the ConnectedThread instance, wouldn’t synchronizing the write function do it (be it inside the ConnectedThread or the method above). I guess it’s possible to call write multiple times from different threads at the same time, but I’ve always seen methods synced, not copies of instances.
It is not for
ConnectedThreadit formStateSynchronize should be used for both read and update.