Home  >  Core Java

Example of AtomicBoolean in Java

By Arvind Rai, December 25, 2012
AtomicBoolean belongs to the package java.util.concurrent.atomic. This package provides lock-free and threads safe handling on a single variable. AtomicBoolean follows the property of volatile values. Object of AtomicBoolean can be created with default value true or false. Find the example.
package com.concretepage;

import java.util.concurrent.atomic.AtomicBoolean;

public class AtomicBooleanTest {
	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){
	   new Thread(new AtomicBooleanTest().new A()).start();
	   new Thread(new AtomicBooleanTest().new B()).start();
   }
}
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us