AtomicBoolean in java


In this thread concurrency tutorial we will learn what is AtomicBoolean in java with program and examples. We will learn important methods of AtomicBoolean like getAndSet, compareAndSet, get, set and how to use them in thread concurrency in java.



Contents of page :
  • What is java.util.concurrent.atomic.AtomicBoolean in java ?
  • AtomicBoolean constructors in java >
  • AtomicBoolean()
  • AtomicBoolean(boolean initialValue)
  • AtomicBoolean important Methods in java >
  • boolean get()
  • void set(boolean newValue)
  • boolean getAndSet(boolean newValue)
  • boolean compareAndSet(boolean expect, boolean update)


What is java.util.concurrent.atomic.AtomicBoolean in java ?
java.util.concurrent.atomic.AtomicBoolean  provides you with boolean value that is updated atomically. i.e. we can use these classes without any explicit synchronization in multithreading environment, because any operation done on these classes is thread safe in java.


AtomicBoolean constructors in java >
  • AtomicBoolean()
Creates a new AtomicBoolean and is initialized to false in java.

Example >
AtomicBoolean atomicBoolean =new AtomicBoolean();
We have created a new AtomicBoolean and it is initialized to false.

  • AtomicBoolean(boolean initialValue)
Creates a new AtomicBoolean and is initialized to specified initialValue.

Example in java >
AtomicBoolean atomicBoolean =new AtomicBoolean(true);
We have created a new AtomicBoolean and it is initialized to true.




AtomicBoolean important Methods in java >
  • boolean get()
method returns the current value

Example >
AtomicBoolean atomicBoolean =new AtomicBoolean();
atomicBoolean.get();
Method will return false.

  • void set(boolean newValue)
Sets to newValue.

Example >
AtomicBoolean atomicBoolean =new AtomicBoolean();
atomicBoolean.set(true);
Method will set return atomicBoolean to true.

  • boolean getAndSet(boolean newValue)
Sets to newValue and returns the old value.

Example >
AtomicBoolean atomicBoolean =new AtomicBoolean();
atomicBoolean.getAndSet(true);
Method will set return atomicBoolean to true. And return false.

  • boolean compareAndSet(boolean expect, boolean update)
Compare with expect, if equal, set to update and return true.
Example >
AtomicBoolean atomicBoolean =new AtomicBoolean(true);
atomicBoolean.compareAndSet(true, false);
Now, in call to  compareAndSet method first parameter [i.e. true] is equal to original value, so method will set AtomicBoolean to false.
And returns true.


So, in this thread concurrency tutorial we learned what is AtomicBoolean in java with program and examples. We learned important methods of AtomicBoolean like getAndSet, compareAndSet, get, set and how to use them in thread concurrency in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


/** JavaMadeSoEasy.com */

RELATED LINKS in java>


Atomic operations >

Atomic operations in java


java.util.concurrent.atomic.AtomicInteger in java

Implementation of custom/own AtomicInteger in java


java.util.concurrent.atomic.AtomicLong in java

Implementation of custom/own AtomicLong in java


AtomicBoolean in java



ReadWriteLock and ReentrantReadWriteLock >

ReadWriteLock and ReentrantReadWriteLock in java

Implementation of custom/own ReadWriteLock and ReentrantReadWriteLock in java



Thread concurrency Interviews >

No comments:

Post a Comment