问题描述
oracle jdk javadoc for atomicboolean状态:
/8/docs/api/java/util/concurrent/atomic/atomicboolean.html
可以在原子上更新的布尔值.看到 java.util.concurrent.Atomic软件包规范用于描述 原子变量的属性.使用Atomicboolean 诸如原子更新标志之类的应用程序,不能用作 替换布尔值.
我和一个同事正在试图找出一个用例,在该用例中,原子博物馆不能成为替代品,我们唯一能想到的是,布尔对象有Atomicicboolean没有的方法.
这是唯一的原因,还是在写作时还有其他想法吗?
推荐答案
Boolean是原始boolean周围的包装类.它可以由编译器(拳击转换)自动创建,也可以转换为布尔值(拆箱转换).对于AtomicBoolean,它不是为并发目的而设计的单独类别的情况.
因此,这两个类在语言层面上具有不同的语义:
Boolean b = new Boolean(true); AtomicBoolean ab = new AtomicBoolean(true); System.out.println(true == b); // automatic unboxing of Boolean variable System.out.println(true == ab); // compiler error
其他推荐答案
布尔值是一个不可变的值对象.它的目的是不变,并最终实现了这一点. java.lang.boolean从1.0开始就出现了.
AtomicBoolean是可变的,旨在更新,以便在线程之间可见更新的值. Java 5引入了Atomicboolean.
这些是完全不同的概念,这就是为什么Atomicboolean并非旨在扩展布尔值的原因.如果不使用它破坏代码的预期不变性,则不能将可变的对象替换为不可变的对象.如果原子版本可以通过其位置,期望收到不变值的代码可能会损坏.
因此,这里有一个用例:如果将atomicicboolean引入了布尔值可以替代的东西,则您可以有一个案例,在这种情况下创建的班级可能会合理地期望以某种返回布尔值的方法,这无需由于布尔不变,通过防御副本.如果返回的引用恰好是从更改使用原子boolean而不是布尔值的来源初始化的,那么现在可以通过将其返回布尔值的方法来修改该字段,并将其施放给Atomicboolean.
原子类设计用于处理并发更新(作为volatile上的改进),但是设计并发代码的最有效方法是使用不可变的值.因此,请注意不要将Atomicboolean误认为是"您在编写多线程代码时使用的布尔值".
其他推荐答案
它们不可自动盒,因此不能在条件下使用,例如
// Explodey if (someAtomicBoolean) { }
问题描述
The Oracle JDK Javadoc for AtomicBoolean states:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicBoolean.html
A boolean value that may be updated atomically. See the java.util.concurrent.atomic package specification for description of the properties of atomic variables. An AtomicBoolean is used in applications such as atomically updated flags, and cannot be used as a replacement for a Boolean.
A colleague and I were trying to figure out a use-case where the AtomicBoolean can't be a substitute and the only thing we can think of is that there are methods the Boolean object has that the AtomicBoolean does not.
Is that the only reason or was there something else in mind when that was written?
推荐答案
Boolean is the wrapper class around the primitive boolean. It may be automatically created from a boolean by the compiler (boxing conversion) or converted to a boolean (unboxing conversion). This is not the case for AtomicBoolean where it is a separate class designed for concurrency purposes.
Hence the two classes have different semantics at the language level:
Boolean b = new Boolean(true); AtomicBoolean ab = new AtomicBoolean(true); System.out.println(true == b); // automatic unboxing of Boolean variable System.out.println(true == ab); // compiler error
其他推荐答案
Boolean is an immutable value object. It was designed to be unchanging and made final in order to enforce that. java.lang.Boolean has been around since 1.0.
AtomicBoolean is mutable, and designed to get updated so that the updated value is visible across threads. AtomicBoolean was introduced with Java 5.
These are entirely different concepts, which is why AtomicBoolean wasn't designed to extend Boolean. You can't substitute a mutable object for an immutable one without wrecking the expected invariants of the code using it. Code expecting to receive an immutable value could get broken if the atomic version could be passed in in its place.
So here's a use case: if AtomicBoolean was introduced as something that was substitutable for Boolean, you could have a case where a class created before this change could reasonably expect that in some method that returns a Boolean it doesn't need to pass a defensive copy on account of Boolean being immutable. If the reference returned happens to get initialized from a source that changes to use AtomicBoolean instead of Boolean, then that field could now be modified by things calling the method returning Boolean, by casting it to AtomicBoolean.
The atomic classes are designed for working with concurrent updates (as an improvement on volatile), but the most efficient way to design concurrent code is to use immutable values. So be careful not to mistake AtomicBoolean for "the Boolean you use when writing multithreaded code".
其他推荐答案
They're not auto-boxable, so they can't be used in conditionals, e.g.,
// Explodey if (someAtomicBoolean) { }