lundi 26 octobre 2015

Why does the next method in Random use a compareAndSet?

While reading through the documentation for the java.util.Random class in Java, i stumbled upon something inside the next method that I couldn't quite get my head around.

protected int next(int bits) {
    long oldseed, nextseed;
    AtomicLong seed = this.seed;
    do {
        oldseed = seed.get();
        nextseed = (oldseed * multiplier + addend) & mask;
    } while (!seed.compareAndSet(oldseed, nextseed));
    return (int)(nextseed >>> (48 - bits));
}

I noticed the use of !seed.compareAndSet(oldseed, nextseed), and I'm trying to understand what it's there for. Can somebody explain it to me?




Aucun commentaire:

Enregistrer un commentaire