0

好的,所以我对为什么在下面标记的行中使用同步感到困惑。

对我来说,你只在一个代码块可能被多个线程访问的地方使用同步,但是这个代码只在它的 run 方法中从这个线程调用。

的一个实例mConnectThread在类的一开始就被声明为一个字段。

public class BluetoothChatService {

// Member fields   
private ConnectThread mConnectThread;

有任何想法吗?

/**
 * This thread runs while attempting to make an outgoing connection
 * with a device. It runs straight through; the connection either
 * succeeds or fails.
 */
private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
    private String mSocketType;

    public ConnectThread(BluetoothDevice device, boolean secure) {
        mmDevice = device;
        BluetoothSocket tmp = null;
        mSocketType = secure ? "Secure" : "Insecure";

        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            if (secure) {
                tmp = device.createRfcommSocketToServiceRecord(
                        MY_UUID_SECURE);
            } else {
                tmp = device.createInsecureRfcommSocketToServiceRecord(
                        MY_UUID_INSECURE);
            }
        } catch (IOException e) {
            Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
        }
        mmSocket = tmp;
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
        setName("ConnectThread" + mSocketType);

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            mmSocket.connect();
        } catch (IOException e) {
            // Close the socket
            try {
                mmSocket.close();
            } catch (IOException e2) {
                Log.e(TAG, "unable to close() " + mSocketType +
                        " socket during connection failure", e2);
            }
            connectionFailed();
            return;
        }

        /********* THIS BIT OF CODE BELOW IS WHAT I AM ASKING ABOUT **********/

        // Reset the ConnectThread because we're done
        synchronized (BluetoothChatService.this) {
            mConnectThread = null;
        }

        /**********************************^^*********************************/

        // Start the connected thread
        connected(mmSocket, mmDevice, mSocketType);
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
        }
    }
}

干杯

4

1 回答 1

1

您可以同时运行多个 ConnectThread 对象,这意味着同一运行方法中的多个线程(从技术上讲,是运行方法的副本,但代码相同),尽管它们都可以访问不同的成员变量。同步块在外部对象上同步,所以我怀疑程序中的其他地方有一个同步块,看起来像

synchronized (BluetoothChatService.this)
{
  if (mConnectThread != null)
    do some work that would throw NPE without the check.
}

编辑:

澄清一下,它们并没有阻止两个线程访问同一个代码块,而是阻止两个线程从不同的代码段访问同一个变量。

于 2013-01-30T16:47:08.307 回答