AtomicBoolean是Java.util.concurrent.atomic包下的原子变量,这个包里面提供了一组原子类,具有排他性 如下例子
当线程thread1 进行操作的的时候thread2,thread3都不能进行操作
public class AtomicDemo {
public static void main(String[] args) {
AtomicBoolean atomicBoolean = new AtomicBoolean(false);
new Thread(new MyRunnable(atomicBoolean, "thread1")).start();
new Thread(new MyRunnable(atomicBoolean, "thread2")).start();
new Thread(new MyRunnable(atomicBoolean, "thread3")).start();
}
}
class MyRunnable implements Runnable {
private AtomicBoolean atomicBoolean;
String name;
public MyRunnable(AtomicBoolean atomicBoolean, String name) {
this.atomicBoolean = atomicBoolean;
this.name = name;
}
@Override
public void run() {
if (atomicBoolean.compareAndSet(false, true)) {
System.out.println(name + " enter");
try {
System.out.println(name + " working");
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// do nothing
}
System.out.println(name + " leave");
atomicBoolean.set(false);
} else {
System.out.println(name + " give up");
}
}
}