I want atomic reads and atomic writes to Integer (not int) and others as follows:
我想要原子读取和原子写入Integer(而不是int)和其他如下:
volatile Double a; // not double
volatile Long b; // not long
Integer c; // not int
Boolean d; // not boolean
If not, how would I make them atomic?
如果没有,我将如何使它们原子化?
Edit: As a clarification, I do not need an atomic operation that combines read and write. I just need the read to be atomic and the write to be atomic, separately.
编辑:作为澄清,我不需要结合读写的原子操作。我只需要将读取设置为原子并将写入分别设置为原子。
Edit2: @Peter Lawrey, @PeterLawrey, This is ironic: Double a; a = 0.5d;
Here the write is atomic because a
is a reference. While in this double a; a = 0.5d;
the write is not be guaranteed to be atomic. And yet the Double
wraps a double
. Of course I would not expect the same to apply to the wrapped contents of arbitrary objects that have references read and written atomicly.
编辑2:@Peter Lawrey,@ PeterLawrey,这很讽刺:双a; a = 0.5d;这里写是原子的,因为a是一个引用。虽然在这个双a; a = 0.5d;写不保证是原子的。然而Double包装了双倍。当然,我不希望将相同的内容应用于具有原子读取和写入引用的任意对象的包装内容。
Edit3: Additional note, I originally wanted to get atomic reads and writes thinking that atomicity ensures consistency across threads. I found that it doesn't and that you still need volatility.
编辑3:补充说明,我原本想要获得原子读写,认为原子性确保了跨线程的一致性。我发现它没有,你仍然需要波动。
2 个解决方案
#1
5
All reads or writes to references are atomic.
对引用的所有读取或写入都是原子的。
I assume you mean you want to do a read and a write atomicly. In that case I would use AtomicReference, AtomicLong, AtomicInteger or AtomicBoolean as approriate . If performance is important to you I would use int
long
or double
instead of wrappers.
我假设你的意思是你想要原子地进行读取和写入。在这种情况下,我会使用AtomicReference,AtomicLong,AtomicInteger或AtomicBoolean作为适当的。如果性能对你很重要,我会使用int long或double而不是wrappers。
For consistency between threads you need volatile
. For atomic operations like increment and swap, you can use AtomicReference, AtomicReferenceFieldUpdater, AtomicLong, AtomicInteger, AtomicBoolean, AtomicIntegerArray, AtomicIntegerFieldUpdater and AtomicLongArray AtomicLongFieldUpdater classes. To perform such atomic operations on a double
, you can build your own updater with Unsafe, but use at your own risk. ;)
为了线程之间的一致性,您需要volatile。对于增量和交换等原子操作,可以使用AtomicReference,AtomicReferenceFieldUpdater,AtomicLong,AtomicInteger,AtomicBoolean,AtomicIntegerArray,AtomicIntegerFieldUpdater和AtomicLongArray AtomicLongFieldUpdater类。要在double上执行此类原子操作,您可以使用Unsafe构建自己的更新程序,但使用风险自负。 ;)
#2
0
Unless I have you mistaken shouldn't you use?
除非我误解你不应该使用?
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/AtomicInteger.html
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/AtomicInteger.html
Instead?
代替?
#1
5
All reads or writes to references are atomic.
对引用的所有读取或写入都是原子的。
I assume you mean you want to do a read and a write atomicly. In that case I would use AtomicReference, AtomicLong, AtomicInteger or AtomicBoolean as approriate . If performance is important to you I would use int
long
or double
instead of wrappers.
我假设你的意思是你想要原子地进行读取和写入。在这种情况下,我会使用AtomicReference,AtomicLong,AtomicInteger或AtomicBoolean作为适当的。如果性能对你很重要,我会使用int long或double而不是wrappers。
For consistency between threads you need volatile
. For atomic operations like increment and swap, you can use AtomicReference, AtomicReferenceFieldUpdater, AtomicLong, AtomicInteger, AtomicBoolean, AtomicIntegerArray, AtomicIntegerFieldUpdater and AtomicLongArray AtomicLongFieldUpdater classes. To perform such atomic operations on a double
, you can build your own updater with Unsafe, but use at your own risk. ;)
为了线程之间的一致性,您需要volatile。对于增量和交换等原子操作,可以使用AtomicReference,AtomicReferenceFieldUpdater,AtomicLong,AtomicInteger,AtomicBoolean,AtomicIntegerArray,AtomicIntegerFieldUpdater和AtomicLongArray AtomicLongFieldUpdater类。要在double上执行此类原子操作,您可以使用Unsafe构建自己的更新程序,但使用风险自负。 ;)
#2
0
Unless I have you mistaken shouldn't you use?
除非我误解你不应该使用?
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/AtomicInteger.html
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/AtomicInteger.html
Instead?
代替?