简体   繁体   English

AtomicBoolean锁在哪里?

[英]AtomicBoolean where is the lock?

AtomicBoolean uses native code for synchronization. AtomicBoolean使用本机代码进行同步。 How does it translate into java locks? 它如何转换成Java锁?

what's the difference between: 之间有什么区别:

AtomicBoolean a = new AtomicBoolean();
synchronized (a) {
   a.set(true);
}

vs: VS:

a.set(true)

I know the synchronized(a) is not needed because a itself will ensure the operation is atomic. 我知道不需要synced(a),因为它本身可以确保操作是原子的。 But is the lock in synchronized (a) the same lock as in a.set(true) ? 但是,同步(a)中的锁是否与a.set(true)中的锁相同?

The atomic relies on the JVM for some if the atomicity, such as in set/get, but relies on the sun.misc.Unsafe class in other cases. 如果是原子性的话,例如在set / get中,原子性就依赖于JVM,但在其他情况下,原子性就依赖于sun.misc.Unsafe类。 You can check out the code at: 您可以在以下位置签出代码:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/concurrent/atomic/AtomicBoolean.java http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/concurrent/atomic/AtomicBoolean.java

It is also worth looking at: 还值得一看:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/misc/Unsafe.java#Unsafe http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/misc/Unsafe.java#Unsafe

which is used for lots of stuff in the JDK, although sadly it is not a public class. 尽管遗憾的是,它不是公共类,但它在JDK中用于很多东西。 Made more sad because it is so obviously named that it could be public and completely "safe" to use ;-) 更让人难过的是,因为它的名字如此明显,以至于它可以公开使用并且完全“安全” ;-)

I should add, the whole idea of the atomic classes is to avoid locks and synchronization, which will often improve performance. 我应该补充一点,原子类的整个想法是避免锁定和同步,这通常会提高性能。 You do not need to use locks to protect them, but may rely on operations like compareAndSwap or incrementAndGet (for numbers) that are not standard when you use a lock. 您不需要使用锁来保护它们,但是可能依赖于使用锁时不标准的操作,例如compareAndSwap或增量AndGet(用于数字)。

AtomicBoolean ,事实上,所有的原子类 ,使用比较并交换 ,以保证atomicitiy。

this: 这个:

a.set(true);

isn't internally synchronized at all , have a look at the code of AtomicBoolean.java in JDK 1.7 (from src.zip): 根本没有内部同步,请查看JDK 1.7中的AtomicBoolean.java代码(来自src.zip):

/**
 * Unconditionally sets to the given value.
 *
 * @param newValue the new value
 */
public final void set(boolean newValue) {
    value = newValue ? 1 : 0;
}

so, yes, it is different to the synchronized version 所以,是的,它不同于同步版本

Synchronization keyword provide three guarantees according to JMM. 同步关键字根据JMM提供三个保证。 1. Atomicity 2. Visibility 3. Reordering 1.原子性2.可见性3.重新排序

but synchronization is blocking in nature. 但是同步本质上是阻塞的。

All the Atomic Classes in java like AtomicInteger, AtomicLong, AtomicBoolean etc also provide the above three guarantees. Java中的所有Atomic类(例如AtomicInteger,AtomicLong,AtomicBoolean等)也提供上述三个保证。 But they dont block other threads. 但是它们不会阻塞其他线程。

They Provide 1. Atomicity - by using compareAndSwap operation. 他们提供1.原子性-通过使用compareAndSwap操作。 2. Visibility and Reordering - these are provide by declaring the underlying variable as volatile. 2.可见性和重新排序-这些是通过将基础变量声明为volatile来提供的。

for example in AtomicInteger the underlying int variable is declared as volatile 例如在AtomicInteger中,将基本int变量声明为volatile

private volatile int value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM