Example usage for java.util.concurrent.atomic AtomicInteger compareAndSet

List of usage examples for java.util.concurrent.atomic AtomicInteger compareAndSet

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicInteger compareAndSet.

Prototype

public final boolean compareAndSet(int expectedValue, int newValue) 

Source Link

Document

Atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle#compareAndSet .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    AtomicInteger atomicInteger = new AtomicInteger();
    atomicInteger.compareAndSet(10, 10);
    System.out.println(atomicInteger.decrementAndGet());
}

From source file:Main.java

static boolean ThrsafeCompareExchange(AtomicInteger paoDestination, int aoComparand, int aoExchange) {
    //return (*paoDestination == aoComparand) ? ((*paoDestination = aoExchange), true) : false;
    return paoDestination.compareAndSet(aoComparand, aoExchange);
}

From source file:AtomicUtils.java

public static boolean tryIncrementIfGreaterThan(final AtomicInteger capacity, final int least) {
    int capa;//from   w ww  .  ja v a  2s.  co m
    do {
        capa = capacity.get();
        if (capa <= least) {
            return false;
        }
    } while (!capacity.compareAndSet(capa, capa + 1));
    return true;
}

From source file:AtomicUtils.java

public static boolean tryDecrementIfGreaterThan(final AtomicInteger capacity, final int least) {
    int capa;/*  w ww  .j  av  a2 s . c o m*/
    do {
        capa = capacity.get();
        if (capa <= least) {
            return false;
        }
    } while (!capacity.compareAndSet(capa, capa - 1));
    return true;
}

From source file:AtomicUtils.java

public static boolean tryIncrementIfLessThan(final AtomicInteger capacity, final int atmost) {
    int capa;/*  w  w w  .j  av  a  2 s  .  co  m*/
    do {
        capa = capacity.get();
        if (capa >= atmost) {
            return false;
        }
    } while (!capacity.compareAndSet(capa, capa + 1));
    return true;
}

From source file:AtomicUtils.java

public static int tryDecrementAndGetIfGreaterThan(final AtomicInteger capacity, final int least) {
    for (;;) {/*from   w w  w  . ja  va 2  s  . c o  m*/
        final int current = capacity.get();
        if (current <= least) {
            return current;
        }
        final int next = current - 1;
        if (capacity.compareAndSet(current, next)) {
            return next;
        }
    }
}

From source file:AtomicUtils.java

public static int tryIncrementAndGetIfLessThan(final AtomicInteger capacity, final int upperbound) {
    for (;;) {/* www.  ja v  a 2s  . c  o m*/
        final int current = capacity.get();
        if (current >= upperbound) {
            return upperbound;
        }
        final int next = current + 1;
        if (capacity.compareAndSet(current, next)) {
            return next;
        }
    }
}

From source file:Main.java

/**
 * Generate a value suitable for use in {@ link #setId(int)}.
 * This value will not collide with ID values generated at build time by aapt for R.id.
 * http://stackoverflow.com/a/15442997/151957
 * @return a generated ID value//from w w w .j a v  a 2 s.c  o m
 */
public static int generateViewId() {
    AtomicInteger sNextGeneratedId = new AtomicInteger(10001);

    for (;;) {
        final int result = sNextGeneratedId.get();
        // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
        int newValue = result + 1;
        if (newValue > 0x00FFFFFF)
            newValue = 1; // Roll over to 1, not 0.
        if (sNextGeneratedId.compareAndSet(result, newValue)) {
            return result;
        }
    }
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static void atomicSetMax(AtomicInteger atomic, int i) {
    while (true) {
        int j = atomic.get();
        if (j >= i || atomic.compareAndSet(j, i))
            break;
    }//from ww w  .ja  va 2s  .c o  m
}

From source file:com.github.cherimojava.data.mongo.io._DeEncoding.java

@Test
public void saveDrop() {
    PrimitiveEntity pe = factory.create(PrimitiveEntity.class);
    factory.create(PrimitiveEntity.class).setString("don't delete").save();
    pe.setString("413").save();
    final AtomicInteger count = new AtomicInteger(0);
    MongoCollection<PrimitiveEntity> coll = getCollection(PrimitiveEntity.class);
    coll.find(PrimitiveEntity.class).forEach(new Block<Entity>() {
        @Override//from   ww w.  ja  v a2  s  .co m
        public void apply(Entity entity) {
            count.getAndIncrement();
        }
    });
    assertEquals(2, count.get());
    pe.drop();

    count.compareAndSet(2, 0);
    coll.find(PrimitiveEntity.class).forEach(new Block<Entity>() {
        @Override
        public void apply(Entity entity) {
            count.getAndIncrement();
        }
    });
    assertEquals(1, count.get());
}