Java Concurrency 简明教程

Java Concurrency - AtomicBoolean Class

java.util.concurrent.atomic.AtomicBoolean 类在可以读取和写入原子的底层布尔值上提供操作,还包含高级原子操作。AtomicBoolean 支持对底层布尔变量的原子操作。它具有像读写 volatile 变量那样的 get 和 set 方法。也就是说,一个 set 与对同一变量的任何后续 get 都具有 happens-before 关系。atomic compareAndSet 方法还具有这些内存一致性特性。

A java.util.concurrent.atomic.AtomicBoolean class provides operations on underlying boolean value that can be read and written atomically, and also contains advanced atomic operations. AtomicBoolean supports atomic operations on underlying boolean variable. It have get and set methods that work like reads and writes on volatile variables. That is, a set has a happens-before relationship with any subsequent get on the same variable. The atomic compareAndSet method also has these memory consistency features.

AtomicBoolean Methods

以下是 AtomicBoolean 类中可用的重要方法列表。

Following is the list of important methods available in the AtomicBoolean class.

Sr.No.

Method & Description

1

public boolean compareAndSet(boolean expect, boolean update) Atomically sets the value to the given updated value if the current value == the expected value.

2

public boolean get() Returns the current value.

3

public boolean getAndSet(boolean newValue) Atomically sets to the given value and returns the previous value.

4

public void lazySet(boolean newValue) Eventually sets to the given value.

5

public void set(boolean newValue) Unconditionally sets to the given value.

6

public String toString() Returns the String representation of the current value.

7

public boolean weakCompareAndSet(boolean expect, boolean update) Atomically sets the value to the given updated value if the current value == the expected value.

Example

以下 TestThread 程序显示了在基于线程的环境中使用 AtomicBoolean 变量。

The following TestThread program shows usage of AtomicBoolean variable in thread based environment.

import java.util.concurrent.atomic.AtomicBoolean;

public class TestThread {

   public static void main(final String[] arguments) throws InterruptedException {
      final AtomicBoolean atomicBoolean = new AtomicBoolean(false);

      new Thread("Thread 1") {

         public void run() {

            while(true) {
               System.out.println(Thread.currentThread().getName()
                  +" Waiting for Thread 2 to set Atomic variable to true. Current value is "
                  + atomicBoolean.get());

               if(atomicBoolean.compareAndSet(true, false)) {
                  System.out.println("Done!");
                  break;
               }
            }
         };
      }.start();

      new Thread("Thread 2") {

         public void run() {
            System.out.println(Thread.currentThread().getName() +
               ", Atomic Variable: " +atomicBoolean.get());
            System.out.println(Thread.currentThread().getName() +
               " is setting the variable to true ");
            atomicBoolean.set(true);
            System.out.println(Thread.currentThread().getName() +
               ", Atomic Variable: " +atomicBoolean.get());
         };
      }.start();
   }
}

这将产生以下结果。

This will produce the following result.

Output

Thread 1 Waiting for Thread 2 to set Atomic variable to true. Current value is false
Thread 1 Waiting for Thread 2 to set Atomic variable to true. Current value is false
Thread 1 Waiting for Thread 2 to set Atomic variable to true. Current value is false
Thread 2, Atomic Variable: false
Thread 1 Waiting for Thread 2 to set Atomic variable to true. Current value is false
Thread 2 is setting the variable to true
Thread 2, Atomic Variable: true
Thread 1 Waiting for Thread 2 to set Atomic variable to true. Current value is false
Done!