java.util.concurrent.atomic.AtomicBoolean Example

In this example, we shall be demonstrating the use of java.util.concurrent.atomic.AtomicBoolean Class added in Java 5.

The AtomicBoolean class is used to update a boolean value atomically i.e. only one thread can modify the boolean variable at a time. It provides two constructors, a parameterless one with default value as false and a constructor with argument as initial boolean value.

It also provides a lazySet(boolean newvalue) method for non-blocking data-structures.(Avoiding costly storeLoad Barriers).

Here is a small program demostrating the use of java.util.concurrent.atomic.AtomicBoolean Class :

package com.javacodegeeks.examples.concurrent;

import java.util.concurrent.atomic.AtomicBoolean;

public class AtomicBooleanExample
	public static void main(String[] args)
		final AtomicBoolean atomicBoolean = new AtomicBoolean(false);
		new Thread("T1")
			public void run()
                                 +" Waiting for T2 to set Atomic variable to true. Current value is "
							if(atomicBoolean.compareAndSet(true, false))
									System.out.println("Finally I can die in peace!");
		new Thread("T2")
			public void run()
						System.out.println(Thread.currentThread().getName() +atomicBoolean.get()); 
                                +" is setting the variable to true ");

T1 Waiting for T2 to set Atomic variable to true. Current value is false
T1 Waiting for T2 to set Atomic variable to true. Current value is false
T2 is setting the variable to true false
T1 Waiting for T2 to set Atomic variable to true. Current value is false
Finally I can die in peace!

In the above example, the AtomicBoolean variable is initialized to false. Thread T1 waits for the other thread T2 to set it to true. Once the thread sets it to true , the thread T1 finally, terminates.

Thus we see that, the AtomicBoolean class offers the same functionality as the volatile variable with the same memory semantics at a far lower cost.

Locks vs AtomicBoolean Performance

Locks perform better, under very high contention, however, AtomicBoolean performs better under low and medium contention.

AtomicBoolean is not a substitute for synchronize. Understand the use case properly before going for either.


Thus, we have studied how we can use the AtomicBoolean Class in our applications.

You can download the source code of this example here:

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Chandan Singh

Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button