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

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

问题描述

我刚刚使用了 org.apache.openjpa.persistence.meta.AnnotationProcessor6 来为我的 JPA2 实体生成元模型.

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.

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

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