原子/挥发/同步内部是如何工作的?
下面的代码块有什么区别?
代码1
private int counter;
public int getNextUniqueIndex() {
return counter++;
}
代码2
private AtomicInteger counter;
public int getNextUniqueIndex() {
return counter.getAndIncrement();
}
代码3
private volatile int counter;
public int getNextUniqueIndex() {
return counter++;
}
挥发物以以下方式工作吗?是
volatile int i = 0;
void incIBy5() {
i += 5;
}
相当于
Integer i = 5;
void incIBy5() {
int temp;
synchronized(i) { temp = i }
synchronized(i) { i = temp + 5 }
}
我认为两个线程不能同时进入同步块…我说的对吗?如果这是真的,那么如何atomic.incrementAndGet()工作没有同步?它是否线程安全?
内部读取和写入volatile变量/原子变量之间的区别是什么?我在一些文章中读到,线程有一个变量的本地副本-那是什么?
volatile +同步是一个完全原子化的操作(语句)的可靠解决方案,它包含了对CPU的多条指令。
Say for eg:volatile int i = 2; i++, which is nothing but i = i + 1; which makes i as the value 3 in the memory after the execution of this statement.
This includes reading the existing value from memory for i(which is 2), load into the CPU accumulator register and do with the calculation by increment the existing value with one(2 + 1 = 3 in accumulator) and then write back that incremented value back to the memory. These operations are not atomic enough though the value is of i is volatile. i being volatile guarantees only that a SINGLE read/write from memory is atomic and not with MULTIPLE. Hence, we need to have synchronized also around i++ to keep it to be fool proof atomic statement. Remember the fact that a statement includes multiple statements.
希望这个解释足够清楚。
将变量声明为volatile意味着修改其值立即影响变量的实际内存存储。编译器不能优化掉对变量的任何引用。这保证当一个线程修改变量时,所有其他线程立即看到新值。(对于非易失性变量,不能保证这一点。)
Declaring an atomic variable guarantees that operations made on the variable occur in an atomic fashion, i.e., that all of the substeps of the operation are completed within the thread they are executed and are not interrupted by other threads. For example, an increment-and-test operation requires the variable to be incremented and then compared to another value; an atomic operation guarantees that both of these steps will be completed as if they were a single indivisible/uninterruptible operation.
对一个变量的所有访问同步一次只允许一个线程访问该变量,并强制所有其他线程等待访问该变量的线程释放其对该变量的访问。
同步访问类似于原子访问,但是原子操作通常在较低的编程级别上实现。此外,完全可以只同步对变量的某些访问,而允许其他访问不同步(例如,同步对变量的所有写操作,但不同步从变量中读取操作)。
原子性、同步性和波动性是独立的属性,但通常结合使用,以加强访问变量时的适当线程合作。
附录(2016年4月)
对变量的同步访问通常使用监视器或信号量实现。这些是低级互斥(互斥)机制,允许线程独占地获得对变量或代码块的控制,如果其他线程也试图获得相同的互斥,则强制所有其他线程等待。一旦拥有这个互斥量的线程释放了这个互斥量,另一个线程就可以依次获得这个互斥量。
附录(2016年7月)
对象发生同步。这意味着调用类的同步方法将锁定调用的This对象。静态同步方法将锁定Class对象本身。
同样,进入同步块需要锁定方法的this对象。
这意味着同步方法(或块)可以同时在多个线程中执行,如果它们锁定在不同的对象上,但是对于任何给定的单个对象,一次只能有一个线程执行同步方法(或块)。
我知道两个线程不能同时进入同步块
两个线程不能两次进入同一个对象的同步块。这意味着两个线程可以在不同的对象上进入相同的块。这种混乱可能导致这样的代码。
private Integer i = 0;
synchronized(i) {
i++;
}
这不会像预期的那样,因为它可能每次都锁定在不同的对象上。
如果这是真的,那么这个atomic.incrementAndGet()如何在没有同步的情况下工作??线程安全吗??
是的。它不使用锁来实现线程安全。
如果您想更详细地了解它们的工作原理,可以阅读它们的代码。
内部读取和写入Volatile变量/原子变量之间的区别是什么?
原子类使用volatile字段。在这个领域没有区别。区别在于所执行的操作。Atomic类使用CompareAndSwap或CAS操作。
我在一些文章中读到线程有变量的本地副本,那是什么?
我只能假设它指的是每个CPU都有自己的缓存内存视图,这可能与其他CPU不同。为了确保CPU拥有一致的数据视图,您需要使用线程安全技术。
这只在共享内存且至少有一个线程更新内存时才会出现问题。