Home  >  Article  >  Java  >  Correct use of Volatile variables in java

Correct use of Volatile variables in java

伊谢尔伦
伊谢尔伦Original
2016-12-05 10:43:111322browse

Volatile variables in the Java language can be seen as a "lesser degree of synchronized"; compared with synchronized blocks, volatile variables require less coding and have less runtime overhead, but they can achieve The function is only part of synchronized. This article describes several patterns of effective use of volatile variables and highlights several situations in which volatile variables are inappropriate.

Locks provide two main features: mutual exclusion and visibility. Mutual exclusion allows only one thread to hold a specific lock at a time, so you can use this feature to implement a coordinated access protocol to shared data, so that only one thread can use the shared data at a time. Visibility is more complex, and must ensure that changes made to the shared data before the lock is released are visible to another thread that subsequently acquires the lock - without the visibility guarantee provided by the synchronization mechanism, what the thread sees Shared variables may have previous values ​​or inconsistent values, which can lead to many serious problems.

Volatile variables

Volatile variables have synchronized visibility properties, but do not have atomic properties. This means that threads can automatically discover the latest value of volatile variables. Volatile variables can be used to provide thread safety, but only for a very limited set of use cases: there are no constraints between multiple variables or between the current value and modified value of a variable. Therefore, volatile alone is not sufficient to implement counters, mutex locks, or any class with invariants related to multiple variables (such as "start <=end").

You may prefer to use volatile variables instead of locks for simplicity or scalability reasons. Certain idioms are easier to code and read when using volatile variables instead of locks. In addition, volatile variables do not block threads like locks, so they rarely cause scalability problems. In some cases, volatile variables can also provide performance advantages over locks if read operations are much greater than write operations.

Conditions for using volatile variables correctly

You can only use volatile variables to replace locks in limited situations. For volatile variables to provide ideal thread safety, the following two conditions must be met at the same time:

The write operation to the variable does not depend on the current value.

This variable is not included in an invariant with other variables.

In effect, these conditions indicate that the valid values ​​that can be written to volatile variables are independent of any program state, including the variable's current state.

The restriction of the first condition prevents volatile variables from being used as thread-safe counters. Although the increment operation (x++) looks like a single operation, it is actually a combined operation consisting of a sequence of read-modify-write operations that must be performed atomically, and volatile cannot provide the necessary atomicity. Achieving correct operation requires that the value of x remains unchanged during the operation, which cannot be achieved with volatile variables. (However, if you adjust the value to only be written from a single thread, you can ignore the first condition.)

Most programming situations will conflict with one of these two conditions, making volatile variables not as common as synchronized Suitable for implementing thread safety. Listing 1 shows a non-thread-safe numeric range class. It contains an invariant - the lower bound is always less than or equal to the upper bound.

Listing 1. Non-thread-safe numeric range class

@NotThreadSafe 
public class NumberRange {    
   private int lower, upper;    
   public int getLower() { return lower; }    
   public int getUpper() { return upper; }    
   public void setLower(int value) { 
        if (value > upper) 
            throw new IllegalArgumentException(...);
        lower = value;
    }    public void setUpper(int value) { 
        if (value < lower) 
            throw new IllegalArgumentException(...);
        upper = value;
    }
}

This approach limits the range of state variables, so defining the lower and upper fields as volatile types cannot fully achieve the thread safety of the class; thus synchronization still needs to be used. Otherwise, if two threads happen to execute setLower and setUpper with inconsistent values ​​at the same time, the scope will be left in an inconsistent state. For example, if the initial state is (0, 5), and at the same time, thread A calls setLower(4) and thread B calls setUpper(3), obviously the values ​​stored cross-deposited by these two operations do not meet the conditions, then the two operations Each thread will pass the check used to protect the invariant, so that the final range value is (4, 3) - an invalid value. As for other operations on ranges, we need to make the setLower() and setUpper() operations atomic - which is not possible by defining the field as volatile.

Performance Considerations

The main reason to use volatile variables is their simplicity: in some cases, using volatile variables is much simpler than using the corresponding lock. The secondary reason for using volatile variables is its performance: in some cases, the performance of the volatile variable synchronization mechanism is better than that of locks.

很难做出准确、全面的评价,例如 “X 总是比 Y 快”,尤其是对 JVM 内在的操作而言。(例如,某些情况下 VM 也许能够完全删除锁机制,这使得我们难以抽象地比较 volatile 和 synchronized 的开销。)就是说,在目前大多数的处理器架构上,volatile 读操作开销非常低 —— 几乎和非 volatile 读操作一样。而 volatile 写操作的开销要比非 volatile 写操作多很多,因为要保证可见性需要实现内存界定(Memory Fence),即便如此,volatile 的总开销仍然要比锁获取低。

volatile 操作不会像锁一样造成阻塞,因此,在能够安全使用 volatile 的情况下,volatile 可以提供一些优于锁的可伸缩特性。如果读操作的次数要远远超过写操作,与锁相比,volatile 变量通常能够减少同步的性能开销。

正确使用 Volatile 的模式

很多并发性专家事实上往往引导用户远离 volatile 变量,因为使用它们要比使用锁更加容易出错。然而,如果谨慎地遵循一些良好定义的模式,就能够在很多场合内安全地使用 volatile 变量。要始终牢记使用 volatile 的限制 —— 只有在状态真正独立于程序内其他内容时才能使用 volatile —— 这条规则能够避免将这些模式扩展到不安全的用例。

模式 #1:状态标志

也许实现 volatile 变量的规范使用仅仅是使用一个布尔状态标志,用于指示发生了一个重要的一次性事件,例如完成初始化或请求停机。

很多应用程序包含了一种控制结构,形式为 “在还没有准备好停止程序时再执行一些工作”,如清单 2 所示:

清单 2. 将 volatile 变量作为状态标志使用

volatile boolean shutdownRequested;
...public void shutdown() { shutdownRequested = true; }public void doWork() { 
    while (!shutdownRequested) { 
        // do stuff
    }
}

很可能会从循环外部调用 shutdown() 方法 —— 即在另一个线程中 —— 因此,需要执行某种同步来确保正确实现 shutdownRequested 变量的可见性。(可能会从 JMX 侦听程序、GUI 事件线程中的操作侦听程序、通过 RMI 、通过一个 Web 服务等调用)。然而,使用synchronized 块编写循环要比使用清单 2 所示的 volatile 状态标志编写麻烦很多。由于 volatile 简化了编码,并且状态标志并不依赖于程序内任何其他状态,因此此处非常适合使用 volatile。

这种类型的状态标记的一个公共特性是:通常只有一种状态转换;shutdownRequested 标志从 false 转换为 true,然后程序停止。这种模式可以扩展到来回转换的状态标志,但是只有在转换周期不被察觉的情况下才能扩展(从 false 到 true,再转换到 false)。此外,还需要某些原子状态转换机制,例如原子变量。

模式 #2:一次性安全发布(one-time safe publication)

缺乏同步会导致无法实现可见性,这使得确定何时写入对象引用而不是原语值变得更加困难。在缺乏同步的情况下,可能会遇到某个对象引用的更新值(由另一个线程写入)和该对象状态的旧值同时存在。(这就是造成著名的双重检查锁定(double-checked-locking)问题的根源,其中对象引用在没有同步的情况下进行读操作,产生的问题是您可能会看到一个更新的引用,但是仍然会通过该引用看到不完全构造的对象)。

实现安全发布对象的一种技术就是将对象引用定义为 volatile 类型。清单 3 展示了一个示例,其中后台线程在启动阶段从数据库加载一些数据。其他代码在能够利用这些数据时,在使用之前将检查这些数据是否曾经发布过。

清单 3. 将 volatile 变量用于一次性安全发布

public class BackgroundFloobleLoader {    
    public volatile Flooble theFlooble;    
    public void initInBackground() {        
    // do lots of stuff
        theFlooble = new Flooble();  // this is the only write to theFlooble
    }
}
public class SomeOtherClass {    
      public void doWork() {        
             while (true) { 
            // do some stuff...
            // use the Flooble, but only if it is ready
            if (floobleLoader.theFlooble != null) 
                doSomething(floobleLoader.theFlooble);
        }
    }
}

如果 theFlooble 引用不是 volatile 类型,doWork()

中的代码在解除对 theFlooble 的引用时,将会得到一个不完全构造的 Flooble。

该模式的一个必要条件是:被发布的对象必须是线程安全的,或者是有效的不可变对象(有效不可变意味着对象的状态在发布之后永远不会被修改)。volatile 类型的引用可以确保对象的发布形式的可见性,但是如果对象的状态在发布后将发生更改,那么就需要额外的同步。

模式 #3:独立观察(independent observation)

安全使用 volatile 的另一种简单模式是:定期 “发布” 观察结果供程序内部使用。例如,假设有一种环境传感器能够感觉环境温度。一个后台线程可能会每隔几秒读取一次该传感器,并更新包含当前文档的 volatile 变量。然后,其他线程可以读取这个变量,从而随时能够看到最新的温度值。

使用该模式的另一种应用程序就是收集程序的统计信息。清单 4 展示了身份验证机制如何记忆最近一次登录的用户的名字。将反复使用lastUser 引用来发布值,以供程序的其他部分使用。

清单 4. 将 volatile 变量用于多个独立观察结果的发布

volatile bean 模式适用于将 JavaBeans 作为“荣誉结构”使用的框架。在 volatile bean 模式中,JavaBean 被用作一组具有 getter 和/或 setter 方法 的独立属性的容器。volatile bean 模式的基本原理是:很多框架为易变数据的持有者(例如 HttpSession)提供了容器,但是放入这些容器中的对象必须是线程安全的。

在 volatile bean 模式中,JavaBean 的所有数据成员都是 volatile 类型的,并且 getter 和 setter 方法必须非常普通 —— 除了获取或设置相应的属性外,不能包含任何逻辑。此外,对于对象引用的数据成员,引用的对象必须是有效不可变的。(这将禁止具有数组值的属性,因为当数组引用被声明为 volatile 时,只有引用而不是数组本身具有 volatile 语义)。对于任何 volatile 变量,不变式或约束都不能包含 JavaBean 属性。清单 5 中的示例展示了遵守 volatile bean 模式的 JavaBean:

清单 5. 遵守 volatile bean 模式的 Person 对象

@ThreadSafe
public class Person {    
  private volatile String firstName;    
  private volatile String lastName;    
  private volatile int age;    
  public String getFirstName() { return firstName; }    
  public String getLastName() { return lastName; }    
  public int getAge() { return age; }    
  public void setFirstName(String firstName) { 
        this.firstName = firstName;
    }    public void setLastName(String lastName) { 
        this.lastName = lastName;
    }    public void setAge(int age) { 
        this.age = age;
    }
}

volatile 的高级模式

前面几节介绍的模式涵盖了大部分的基本用例,在这些模式中使用 volatile 非常有用并且简单。这一节将介绍一种更加高级的模式,在该模式中,volatile 将提供性能或可伸缩性优势。

volatile 应用的的高级模式非常脆弱。因此,必须对假设的条件仔细证明,并且这些模式被严格地封装了起来,因为即使非常小的更改也会损坏您的代码!同样,使用更高级的 volatile 用例的原因是它能够提升性能,确保在开始应用高级模式之前,真正确定需要实现这种性能获益。需要对这些模式进行权衡,放弃可读性或可维护性来换取可能的性能收益 —— 如果您不需要提升性能(或者不能够通过一个严格的测试程序证明您需要它),那么这很可能是一次糟糕的交易,因为您很可能会得不偿失,换来的东西要比放弃的东西价值更低。

模式 #5:开销较低的读-写锁策略

目前为止,您应该了解了 volatile 的功能还不足以实现计数器。因为 ++x 实际上是三种操作(读、添加、存储)的简单组合,如果多个线程凑巧试图同时对 volatile 计数器执行增量操作,那么它的更新值有可能会丢失。

然而,如果读操作远远超过写操作,您可以结合使用内部锁和 volatile 变量来减少公共代码路径的开销。清单 6 中显示的线程安全的计数器使用synchronized 确保增量操作是原子的,并使用 volatile 保证当前结果的可见性。如果更新不频繁的话,该方法可实现更好的性能,因为读路径的开销仅仅涉及 volatile 读操作,这通常要优于一个无竞争的锁获取的开销。

清单 6. 结合使用 volatile 和 synchronized 实现 “开销较低的读-写锁”

@ThreadSafe
public class CheesyCounter {    
      // Employs the cheap read-write lock trick
    // All mutative operations MUST be done with the &#39;this&#39; lock held
    @GuardedBy("this")    
    private volatile int value;    
    public int getValue() {        
       return value;
    }    
    public synchronized int increment() {        
       return value++;
    }
}

之所以将这种技术称之为 “开销较低的读-写锁” 是因为您使用了不同的同步机制进行读写操作。因为本例中的写操作违反了使用 volatile 的第一个条件,因此不能使用 volatile 安全地实现计数器 —— 您必须使用锁。然而,您可以在读操作中使用 volatile 确保当前值的 可见性 ,因此可以使用锁进行所有变化的操作,使用 volatile 进行只读操作。其中,锁一次只允许一个线程访问值,volatile 允许多个线程执行读操作,因此当使用 volatile 保证读代码路径时,要比使用锁执行全部代码路径获得更高的共享度 —— 就像读-写操作一样。然而,要随时牢记这种模式的弱点:如果超越了该模式的最基本应用,结合这两个竞争的同步机制将变得非常困难。

结束语

与锁相比,volatile 变量是一种非常简单但同时又非常脆弱的同步机制,它在某些情况下将提供优于锁的性能和伸缩性。如果严格遵循 volatile 的使用条件 —— 即变量真正独立于其他变量和自己以前的值 —— 在某些情况下可以使用 volatile 代替 synchronized 来简化代码。然而,使用 volatile 的代码往往比使用锁的代码更加容易出错。本文介绍的模式涵盖了可以使用 volatile 代 synchronized 的最常见的一些用例。遵循这些模式(注意使用时不要超过各自的限制)可以帮助您安全地实现大多数用例,使用 volatile 变量获得更佳性能。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:java common keywordsNext article:java common keywords