atomicreference

A thread-safe holder for arbitrary cloneable data

只愿长相守 提交于 2020-01-25 07:39:06
问题 I have a class SomeMutableData with a public clone() method. I want to make sure, that no thread ever sees an inconsistent state (assuming the instances will be passed around using the holder only). I assume using synchronization is the safest possibility, right? public final class ThreadSafeHolder { public ThreadSafeHolder(SomeMutableData data) { storeData(data); } public synchronized SomeMutableData cloneData() { return data.clone(); } public synchronized void storeData(SomeMutableData data

multiple fields: volatile or AtomicReference?

独自空忆成欢 提交于 2019-12-10 15:44:04
问题 I have to synchronize access between threads to a shared object, whose state consists of several fields. Say: class Shared{ String a; Integer b; //constructor, getters and setters .... } I have possibly many threads reading this objects, doing //readers shared.getA(); shared.getB(); and only one thread that will write at a certain point: //writer shared.setA("state"); shared.setB(1); now my question is how to ensure that reading threads won't find the shared object in an inconsistent state. I

Possible to safely increment BigInteger in a thread safe way, perhaps with AtomicReference, w/o locking?

◇◆丶佛笑我妖孽 提交于 2019-12-04 10:25:19
问题 A lot of our code is legacy but we are moving to a "Big Data" back-end and I'm trying to evangelize the newer API calls, encourage the use of the latest Spring libraries etc. One of our problems is application layer ID generation. For reasons I don't understand, a higher authority wants sequential BigInteger's. I would have made them random with re-generate and re-try on failed insertions but I done got vetoed. Grumbling aside, I'm in a position where I need to increment and get a BigInteger

Possible to safely increment BigInteger in a thread safe way, perhaps with AtomicReference, w/o locking?

不打扰是莪最后的温柔 提交于 2019-12-03 05:51:17
A lot of our code is legacy but we are moving to a "Big Data" back-end and I'm trying to evangelize the newer API calls, encourage the use of the latest Spring libraries etc. One of our problems is application layer ID generation. For reasons I don't understand, a higher authority wants sequential BigInteger's. I would have made them random with re-generate and re-try on failed insertions but I done got vetoed. Grumbling aside, I'm in a position where I need to increment and get a BigInteger across threads and do it in a safe and performant manner. I've never used AtomicReference before but it