2015-01-08 3 views
0

이 널 (null) 과제의 목적은 무엇인가. connect 메서드에서 BluetoothChatService 클래스 (https://developer.android.com/samples/BluetoothChat/src/com.example.android.bluetoothchat/BluetoothChatService.html)를 찾고 있습니다. - 어쨌든, 단 몇 줄의 나중에 mConnectThread난 그냥 안드로이드 샘플 응용 프로그램 중 하나 분석하고있어 안드로이드 샘플 블루투스 채팅 응용 프로그램

mConnectThread = null; 

그것은이 라인은 쓸모없는 것 같다

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); 
} 

나는이 라인의 목적이 무엇인가 이해가 안 :이 코드의 같은 부분이 있습니다 새로운 가치로 덮어 씁니다.

답변

1

그것은이 새로운 값으로 설정되기 전에 경우에 예외가 발생합니다,이 코드의 앞부분에서 null로 mConnectThread을 설정하는 것이 안전합니다. 이렇게하면 새 값이 할당되었는지 여부에 관계없이 이전 인스턴스를 가비지 수집에 사용할 수 있습니다.

그러나, 하나는 확실히이 방법에서 행동의 더 나은 시퀀스 주장 할 수 있습니다. 일반적으로 당신 말이 맞습니다. 새로운 값을 할당하기 직전에 null로 설정하는 것이별로 중요하지 않습니다.