为什么使用volatile成员生成JPA2 MetaModel? [英] Why do the JPA2 MetaModel get generated with volatile members?

查看:163
本文介绍了为什么使用volatile成员生成JPA2 MetaModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用 org.apache.openjpa.persistence.meta.AnnotationProcessor6 为我的JPA2实体生成MetaModel。

I have just used org.apache.openjpa.persistence.meta.AnnotationProcessor6 to generate the MetaModel for my JPA2 entities.

@javax.annotation.Generated
(value="org.apache.openjpa.persistence.meta.AnnotationProcessor6",
   date="Tue Nov 22 09:49:03 CET 2011")
public class Entity_ {
    public static volatile SingularAttribute<Entity,Entity> id;
    public static volatile SingularAttribute<Entity,String> value;
    public static volatile SingularAttribute<Entity,String> order;
}

在这种情况下,有人可以解释为什么属性标记为volatile?

Can someone please explain why the attributes are marked volatile in this case?

谢谢。

推荐答案

设置静态变量的线程可能不是与用于访问它们的线程相同,因此需要 volatile 修饰符来同步所有线程之间的内存。

The thread that sets the static variables might not be the same as the thread that you use to access them, so the volatile modifier is required to synchronize memory between all threads.

没有 volatile 的方案是这样的:


  1. 您的线程访问初始化JPA提供程序之前的变量,静态字段获取 null

  2. JPA提供程序从不同的线程初始化并设置静态字段到非空值

  3. 您的线程再次访问静态字段。在这种情况下,线程的缓存内存将不会看到更改,并继续为所有静态字段返回 null

  1. Your thread accesses the variables before the JPA provider is initialized and gets null for the static fields
  2. The JPA provider is initialized from a different thread and sets the static fields to non-null values
  3. Your thread accesses the static fields again. In this case the cached memory of your thread will not see the changes and continue to return null for all static fields.

这篇关于为什么使用volatile成员生成JPA2 MetaModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆