Android - BluetoothChat示例 - 为什么他们在这种情况下使用了synchronized?

时间:2013-01-30 16:37:07

标签: java android multithreading deadlock synchronized

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

对我来说,你只使用synchronized,多个线程可能会访问一段代码,但是这个代码只能在它的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);
        }
    }
}

干杯

1 个答案:

答案 0 :(得分:1)

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

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

编辑:

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