I'm just analyzing one of android sample applications - the bluetooth chat: https://developer.android.com/samples/BluetoothChat/project.html . I'm looking at the BluetoothChatService class ( https://developer.android.com/samples/BluetoothChat/src/com.example.android.bluetoothchat/BluetoothChatService.html ), at the connect method. There is such piece of code there:
public synchronized void connect(BluetoothDevice device, boolean secure) {
Log.d("@@@", "connect to: " + device);
// Cancel any thread attempting to make a connection
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
// Start the thread to connect with the given device
mConnectThread = new ConnectThread(device, secure);
mConnectThread.start();
setState(STATE_CONNECTING);
}
I don't understand what is the purpose of this line:
mConnectThread = null;
It seems this line is useless - anyway, just a few lines later mConnectThread is overwritten with new value.
It is safer to set mConnectThread
to null earlier in this code, in case an exception is thrown before it has been set to a new value. This way the old instance is available for garbage collection regardless of whether a new value is assigned.
However, one could certainly argue for a better sequence of actions in this method. Generally you're right, there wouldn't be much point in setting it to null just before assigning a new value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.