0
在进行OkHttp调用时,是否需要担心回调方法中的内存一致性?OkHttp队列中的内存一致性回调方法?我需要易变吗?
下面是我问的一个例子。在主线程中n被设置为5,并且没有其他写入到n。然后在onResponse中访问,它在另一个线程上运行。我们保证当在另一个线程上阅读时,n的值是5(如果是这样,为什么)?或者我们需要让n变易?
int n;
n = 5; // This is the only write to n. It happens on the main thread.
mOkHttpCall.enqueue(new Callback() {
@Override
public void onResponse(@Nonnull Call call, @Nonnull Response response) throws IOException
{
// We're not on the main thread here. Is n guaranteed to be 5?
}
});