AtomicBoolean

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 :

AtomicBooleanExample.java:

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()
			{
				while(true){
							System.out.println(Thread.currentThread().getName() 
                                 +" Waiting for T2 to set Atomic variable to true. Current value is "
                                 +atomicBoolean.get());
							if(atomicBoolean.compareAndSet(true, false))
							{
									System.out.println("Finally I can die in peace!");
									break;
							}
			}};
		}.start();
		new Thread("T2")
		{
			public void run()
			{
						System.out.println(Thread.currentThread().getName() +atomicBoolean.get()); 
						System.out.println(Thread.currentThread().getName()
                                +" is setting the variable to true ");
						atomicBoolean.set(true);
			};
		}.start();
}
}

OUTPUT  : 
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
T2false
T2 is setting the variable to true false
T2true
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.

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

Conclusion

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

Download
You can download the source code of this example here: AtomicBooleanExample.zip

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button