加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 线程的多线程访问和变量缓存

发布时间:2020-12-14 05:46:57 所属栏目:Java 来源:网络整理
导读:如果我读完一篇关于多线程的完整章节/书,我就能找到答案,但我想要一个更快的答案. (我知道 this stackoverflow问题类似,但还不够.) 假设有这个类: public class TestClass { private int someValue; public int getSomeValue() { return someValue; } publi
如果我读完一篇关于多线程的完整章节/书,我就能找到答案,但我想要一个更快的答案. (我知道 this stackoverflow问题类似,但还不够.)

假设有这个类:

public class TestClass {
   private int someValue;

   public int getSomeValue() { return someValue; }
   public void setSomeValue(int value) {  someValue = value; }
}

有两个线程(A和B)访问此类的实例.请考虑以下顺序:

> A:getSomeValue()
> B:setSomeValue()
> A:getSomeValue()

如果我是对的,someValue必须是volatile,否则第3步可能不会返回最新值(因为A可能有缓存值).它是否正确?

第二种情况:

> B:setSomeValue()
> A:getSomeValue()

在这种情况下,A将始终获得正确的值,因为这是它的第一次访问,因此他还没有缓存值.这是正确的吗?

如果只以第二种方式访问??类,则不需要volatile / synchronization,或者是它?

请注意,这个例子是简化的,实际上我想知道复杂类中的特定成员变量和方法,而不是整个类(即哪些变量应该是易失性的或具有同步访问权).重点是:如果更多线程访问某些数据,是否需要通过所有方式进行同步访问,还是取决于它们访问它的方式(例如订单)?

阅读评论后,我尝试用另一个例子来说明我的困惑的来源:

>来自UI线程:threadA.start()
> threadA调用getSomeValue(),并通知UI线程
> UI线程获取消息(在其消息队列中),因此它调用:threadB.start()
> threadB调用setSomeValue(),并通知UI线程
> UI线程获取消息,并通知threadA(以某种方式,例如消息队列)
> threadA调用getSomeValue()

这是一个完全同步的结构,但为什么这意味着threadA将在步骤6中获得最新的值? (如果someValue不是易失性的,或者从任何地方访问时都没有放入监视器)

解决方法

问题是java只是一个规范.有许多JVM实现和物理操作环境的示例.在任何给定的组合上,动作可能是安全的或不安全的.例如,在单处理器系统上,示例中的volatile关键字可能完全没必要.由于存储器和语言规范的编写者无法合理地考虑可能的操作条件集,因此他们选择将某些模式列入白名单,这些模式可以保证适用于所有兼容的实现.遵守这些准则可确保您的代码可以在目标系统上运行,并且可以合理地移植.

在这种情况下,“缓存”通常是指在硬件级别上进行的活动. java中存在某些事件导致多处理器系统上的核心“同步”其缓存.访问volatile变量就是一个例子,synchronized块是另一个.想象一下这两个线程X和Y被安排在不同处理器上运行的场景.

X starts and is scheduled on proc 1
y starts and is scheduled on proc 2

.. now you have two threads executing simultaneously
to speed things up the processors check local caches
before going to main memory because its expensive.

x calls setSomeValue('x-value') //assuming proc 1's cache is empty the cache is set
                                //this value is dropped on the bus to be flushed
                                //to main memory
                                //now all get's will retrieve from cache instead
                                //of engaging the memory bus to go to main memory 
y calls setSomeValue('y-value') //same thing happens for proc 2

//Now in this situation depending on to order in which things are scheduled and
//what thread you are calling from calls to getSomeValue() may return 'x-value' or
//'y-value. The results are completely unpredictable.

关键是volatile(在兼容的实现上)确保有序写入将始终刷新到主存储器,并且在下次访问之前,其他处理器的高速缓存将被标记为“脏”,而不管发生该访问的线程.

免责声明:volatile不会锁定.这在以下情况下尤为重要:

volatile int counter;

public incrementSomeValue(){
    counter++; // Bad thread juju - this is at least three instructions 
               // read - increment - write             
               // there is no guarantee that this operation is atomic
}

如果您的意图是必须始终在getSomeValue之前调用setSomeValue,这可能与您的问题相关

如果意图是getSomeValue()必须始终反映最近对setSomeValue()的调用,那么这是使用volatile关键字的好地方.请记住,如果没有它,即使首先调度了setSomeValue(),也无法保证getSomeValue()将反映最近对setSomeValue()的调用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读