Home  >  Core Java

Java AtomicBoolean Example

By Arvind Rai, May 11, 2022
On this page we will learn using Java AtomicBoolean class.
1. Java AtomicBoolean is a boolean value that may be updated atomically.
2. The AtomicBoolean is used in the application where we need atomically updated flags. We should use it in the application when we have multiple threads accessing a boolean variable.
3. The AtomicBoolean variable is lock-free and thread-safe. Instances of AtomicBoolean maintain values that are accessed and updated using its methods.
4. The AtomicBoolean should not be used as the replacement of Boolean.
5. The AtomicBoolean is introduced since Java 5.
6. In our example, we will discuss getAndSet and compareAndSet methods of AtomicBoolean class.



Creating Instance

The instances of AtomicBoolean can be created in two ways.
1. With initial default value as false.
AtomicBoolean ab = new AtomicBoolean(); 
2. With given initial value.
AtomicBoolean(boolean initialValue) 
We can create instance as following.
AtomicBoolean ab = new AtomicBoolean(true); 

Example-1: getAndSet

Find the getAndSet declaration from Java doc.
final boolean getAndSet(boolean newValue) 
Atomically sets the value to newValue and returns the old value.
Find the example.
GetAndSetDemo.java
package com.concretepage;
import java.util.concurrent.atomic.AtomicBoolean;
public class GetAndSetDemo {
  public static void main(String[] args) {
     AtomicBoolean ab1 = new AtomicBoolean(true);
     boolean oldVal1 = ab1.getAndSet(false);
     System.out.println(ab1.get()); //false
     System.out.println(oldVal1); //true

     AtomicBoolean ab2 = new AtomicBoolean(false);
     boolean oldVal2 = ab2.getAndSet(true);
     System.out.println(ab2.get()); //true
     System.out.println(oldVal2); //false     
  }
} 

Example-2: Using compareAndSet()

Find the compareAndSet declaration from Java doc.
final boolean compareAndSet(boolean expectedValue, boolean newValue) 
Atomically sets the value to newValue if the current value == expectedValue.
The method returns true if successful.
Find the example.
CompareAndSetDemo.java
package com.concretepage;
import java.util.concurrent.atomic.AtomicBoolean;
public class CompareAndSetDemo {
  public static void main(String[] args) {
     AtomicBoolean ab = new AtomicBoolean(true);
     System.out.println(ab.get()); //true
     
     boolean isSuccess = ab.compareAndSet(false, false); //(current value != expectedValue)
     System.out.println(isSuccess); //false
     System.out.println(ab.get()); //true (Value not changed)
     
     isSuccess = ab.compareAndSet(true, false); // current value == expectedValue
     System.out.println(isSuccess); //true
     System.out.println(ab.get()); //false (Value changed)    
  }
} 

Example-3

In this example, we will use AtomicBoolean with multi-threads.
WithMultiThreads.java
package com.concretepage;
import java.util.concurrent.atomic.AtomicBoolean;
public class WithMultiThreads {
  AtomicBoolean ab = new AtomicBoolean(true);
  class A implements Runnable {
	@Override
	public void run() {
	  ab.compareAndSet(false, true);
	  System.out.println(ab.get());
	}
  }
  class B implements Runnable {
	@Override
	public void run() {
	  ab.compareAndSet(true, false);
	  System.out.println(ab.get());
	}
  }
  public static void main(String... args) {
	WithMultiThreads ob = new WithMultiThreads();
	new Thread(ob.new A()).start();
	new Thread(ob.new B()).start();
  }
} 

Reference

Class AtomicBoolean
POSTED BY
ARVIND RAI
ARVIND RAI










©2022 concretepage.com | Privacy Policy | Contact Us