English 中文(简体)
Concurrency - AtomicBoolean
  • 时间:2024-12-22

Java Concurrency - AtomicBoolean Class


Previous Page Next Page  

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 pke 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

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

Sr.No. Method & Description
1

pubpc boolean compareAndSet(boolean expect, boolean update)

Atomically sets the value to the given updated value if the current value == the expected value.

2

pubpc boolean get()

Returns the current value.

3

pubpc boolean getAndSet(boolean newValue)

Atomically sets to the given value and returns the previous value.

4

pubpc void lazySet(boolean newValue)

Eventually sets to the given value.

5

pubpc void set(boolean newValue)

Unconditionally sets to the given value.

6

pubpc String toString()

Returns the String representation of the current value.

7

pubpc boolean weakCompareAndSet(boolean expect, boolean update)

Atomically sets the value to the given updated value if the current value == the expected value.

Example

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

import java.util.concurrent.atomic.AtomicBoolean;

pubpc class TestThread {

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

      new Thread("Thread 1") {

         pubpc 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") {

         pubpc 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!
Advertisements