这种同步化的意义何在?[英] What's the point of this synchronization?

本文是小编为大家收集整理的关于这种同步化的意义何在?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这里同步的意义是什么?

为什么不使用mConnectedThread.write(out)?

代码段来自Android (在此处找到)

    /**
 * Write to the ConnectedThread in an unsynchronized manner
 * @param out The bytes to write
 * @see ConnectedThread#write(byte[])
 */
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);
}

推荐答案

需要同步以确保您没有不一致的状态.

没有同步,代码将是:

public void write(byte[] out) {
    if (mState != STATE_CONNECTED) return;
    mConnectedThread.write(out);
}

现在,如果在方法调用执行之前,可以将连接在if语句检查和方法调用之间关闭的连接,则可以将其分配给NULL.这将导致NullPointerException.

其他推荐答案

每当两个线程访问相同的数据(在此处,变量mState和mConnectedThread)时,它们必须使用必须使用确保可见性的"内存屏障".看来mState和mConnectedThread的原子性在这里也很重要.

可见性意味着一个线程所做的更改将对另一个线程可见.优化可能会导致一个值缓存,以便只能看到使其螺纹可见的更改.同步会导致任何写作反映在主内存中,并且任何读取以绕过本地缓存并在主内存中制作.从理论上讲,如果没有同步,一个线程可以设置mState和mConnectedThread,但是其他线程可能永远不会"看到"这些更改,并永远等待该条件更改.

原子性意味着无法单独观察多个动作.从另一线程的角度来看,没有发生任何更改,或者所有更改都发生了.因此,例如,另一个线程永远看不到mState是mConnectedThread,而是在分配之前读取mConnectedThread.

其他推荐答案

需要锁定才能保持一致性. 将mconnectedThread复制到一个单独的变量是因为可以在锁外进行写入 - 可能是一个潜在的长度操作.

本文地址:https://www.itbaoku.cn/post/102755.html

问题描述

What is the point of the synchronization here?

Why not just use mConnectedThread.write(out)?

The code snippet is from the BluetoothChat sample for Android (found here)

    /**
 * Write to the ConnectedThread in an unsynchronized manner
 * @param out The bytes to write
 * @see ConnectedThread#write(byte[])
 */
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);
}

推荐答案

Synchronization is needed to ensure that you don't have inconsistent state.

Without the synchronization, the code would be:

public void write(byte[] out) {
    if (mState != STATE_CONNECTED) return;
    mConnectedThread.write(out);
}

Now, if the connection where to close between the if statement check and the method invocation, mConnectedThread might be assigned to null, before the method invocation execution. That would result into a NullPointerException.

其他推荐答案

Whenever two threads access the same data (here, the variables mState and mConnectedThread), they must use a "memory barrier" that ensures visibility. It appears that the atomicity of mState and mConnectedThread are important here too.

Visibility means that changes made by one thread will be visible to another. Optimizations might cause a value to be cached so that changes are only visible to the thread that made them. Synchronization causes any writes to be reflected in main memory, and any reads to bypass local caches and be made against main memory. In theory, without synchronization one thread could set mState and mConnectedThread, but other threads might never "see" those changes, and wait forever for that condition to change.

Atomicity means that multiple actions cannot be observed individually. From another thread's point of view, none of the changes have occurred, or all of the changes have occurred. So, for example, another thread could never see that mState is STATE_CONNECTED, but read mConnectedThread before it has been assigned.

其他推荐答案

The lock is needed for consistency. Copying mConnectedThread to a separate variable is because then the write - which is a potentially length operation - can be done outside of the lock.