Java Code Examples for java.util.concurrent.atomic.AtomicLongArray#set()

The following examples show how to use java.util.concurrent.atomic.AtomicLongArray#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AtomicLongArrayTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws InterruptedException {
    final AtomicLongArray a = new AtomicLongArray(1);
    a.set(0, 1);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!a.compareAndSet(0, 2, 3))
                Thread.yield();
        }});

    t.start();
    assertTrue(a.compareAndSet(0, 1, 2));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertEquals(3, a.get(0));
}
 
Example 2
Source File: AtomicLongArrayTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws InterruptedException {
    final AtomicLongArray a = new AtomicLongArray(1);
    a.set(0, 1);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!a.compareAndSet(0, 2, 3))
                Thread.yield();
        }});

    t.start();
    assertTrue(a.compareAndSet(0, 1, 2));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertEquals(3, a.get(0));
}
 
Example 3
Source File: AtomicLongArrayTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * addAndGet adds given value to current, and returns current value
 */
public void testAddAndGet() {
    AtomicLongArray aa = new AtomicLongArray(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, 1);
        assertEquals(3, aa.addAndGet(i, 2));
        assertEquals(3, aa.get(i));
        assertEquals(-1, aa.addAndGet(i, -4));
        assertEquals(-1, aa.get(i));
    }
}
 
Example 4
Source File: AtomicLongArrayTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * repeated weakCompareAndSet succeeds in changing value when equal
 * to expected
 */
public void testWeakCompareAndSet() {
    AtomicLongArray aa = new AtomicLongArray(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, 1);
        do {} while (!aa.weakCompareAndSet(i, 1, 2));
        do {} while (!aa.weakCompareAndSet(i, 2, -4));
        assertEquals(-4, aa.get(i));
        do {} while (!aa.weakCompareAndSet(i, -4, 7));
        assertEquals(7, aa.get(i));
    }
}
 
Example 5
Source File: AtomicLongArrayTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * a deserialized serialized array holds same values
 */
public void testSerialization() throws Exception {
    AtomicLongArray x = new AtomicLongArray(SIZE);
    for (int i = 0; i < SIZE; i++)
        x.set(i, -i);
    AtomicLongArray y = serialClone(x);
    assertNotSame(x, y);
    assertEquals(x.length(), y.length());
    for (int i = 0; i < SIZE; i++) {
        assertEquals(x.get(i), y.get(i));
    }
}
 
Example 6
Source File: Atomic8Test.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * AtomicLongArray getAndUpdate returns previous value and updates
 * result of supplied function
 */
public void testLongArrayGetAndUpdate() {
    AtomicLongArray a = new AtomicLongArray(1);
    a.set(0, 1);
    assertEquals(1L, a.getAndUpdate(0, Atomic8Test::addLong17));
    assertEquals(18L, a.getAndUpdate(0, Atomic8Test::addLong17));
    assertEquals(35L, a.get(0));
}
 
Example 7
Source File: AtomicLongArrayTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Multiple threads using same array of counters successfully
 * update a number of times equal to total count
 */
public void testCountingInMultipleThreads() throws InterruptedException {
    final AtomicLongArray aa = new AtomicLongArray(SIZE);
    long countdown = 10000;
    for (int i = 0; i < SIZE; i++)
        aa.set(i, countdown);
    Counter c1 = new Counter(aa);
    Counter c2 = new Counter(aa);
    Thread t1 = newStartedThread(c1);
    Thread t2 = newStartedThread(c2);
    t1.join();
    t2.join();
    assertEquals(c1.decs + c2.decs, SIZE * countdown);
}
 
Example 8
Source File: AtomicLongArray9Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * repeated weakCompareAndSetPlain succeeds in changing value when equal
 * to expected
 */
public void testWeakCompareAndSetPlain() {
    AtomicLongArray aa = new AtomicLongArray(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, 1);
        do {} while (!aa.weakCompareAndSetPlain(i, 1, 2));
        do {} while (!aa.weakCompareAndSetPlain(i, 2, -4));
        assertEquals(-4, aa.get(i));
        do {} while (!aa.weakCompareAndSetPlain(i, -4, 7));
        assertEquals(7, aa.get(i));
    }
}
 
Example 9
Source File: AtomicLongArrayTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getAndIncrement returns previous value and increments
 */
public void testGetAndIncrement() {
    AtomicLongArray aa = new AtomicLongArray(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, 1);
        assertEquals(1, aa.getAndIncrement(i));
        assertEquals(2, aa.get(i));
        aa.set(i, -2);
        assertEquals(-2, aa.getAndIncrement(i));
        assertEquals(-1, aa.getAndIncrement(i));
        assertEquals(0, aa.getAndIncrement(i));
        assertEquals(1, aa.get(i));
    }
}
 
Example 10
Source File: HeapChunkSpace.java    From greycat with Apache License 2.0 5 votes vote down vote up
public HeapChunkSpace(final int initialCapacity, final int batchSize, final Graph p_graph, final boolean deepWorldPriority) {
    _interceptors = null;
    _batchSize = batchSize;
    _deep_priority = deepWorldPriority;
    _graph = p_graph;
    _maxEntries = initialCapacity;
    _hashEntries = initialCapacity * HASH_LOAD_FACTOR;
    _lru = new HeapFixedStack(initialCapacity, true);
    _dirtiesStack = new HeapFixedStack(initialCapacity, false);
    _hashNext = new AtomicIntegerArray(initialCapacity);
    _hash = new AtomicIntegerArray(_hashEntries);
    for (int i = 0; i < initialCapacity; i++) {
        _hashNext.set(i, -1);
    }
    for (int i = 0; i < _hashEntries; i++) {
        _hash.set(i, -1);
    }
    _chunkValues = new AtomicReferenceArray<Chunk>(initialCapacity);
    _chunkWorlds = new AtomicLongArray(_maxEntries);
    _chunkTimes = new AtomicLongArray(_maxEntries);
    _chunkIds = new AtomicLongArray(_maxEntries);
    _chunkTypes = new HeapAtomicByteArray(_maxEntries);
    _chunkMarks = new AtomicLongArray(_maxEntries);
    for (int i = 0; i < _maxEntries; i++) {
        _chunkMarks.set(i, 0);
    }
}
 
Example 11
Source File: AtomicLongArrayTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getAndSet returns previous value and sets to given value at given index
 */
public void testGetAndSet() {
    AtomicLongArray aa = new AtomicLongArray(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, 1);
        assertEquals(1, aa.getAndSet(i, 0));
        assertEquals(0, aa.getAndSet(i, -10));
        assertEquals(-10, aa.getAndSet(i, 1));
    }
}
 
Example 12
Source File: AtomicLongArray9Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * getAcquire returns the last value set
 */
public void testGetAcquireSet() {
    AtomicLongArray aa = new AtomicLongArray(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, 1);
        assertEquals(1, aa.getAcquire(i));
        aa.set(i, 2);
        assertEquals(2, aa.getAcquire(i));
        aa.set(i, -3);
        assertEquals(-3, aa.getAcquire(i));
    }
}
 
Example 13
Source File: ByteArrayOrdinalMap.java    From zeno with Apache License 2.0 5 votes vote down vote up
/**
 * Hash all of the existing values specified by the keys in the supplied long array
 * into the supplied AtomicLongArray.
 */
private void populateNewHashArray(AtomicLongArray newKeys, long[] valuesToAdd) {
    int modBitmask = newKeys.length() - 1;

    for(int i=0;i<valuesToAdd.length;i++) {
        if(valuesToAdd[i] != EMPTY_BUCKET_VALUE) {
            int hash = rehashPreviouslyAddedData(valuesToAdd[i]);
            int bucket = hash & modBitmask;
            while(newKeys.get(bucket) != EMPTY_BUCKET_VALUE)
                bucket = (bucket + 1) & modBitmask;
            newKeys.set(bucket, valuesToAdd[i]);
        }
    }
}
 
Example 14
Source File: AtomicLongArrayTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * getAndAdd returns previous value and adds given value
 */
public void testGetAndAdd() {
    AtomicLongArray aa = new AtomicLongArray(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, 1);
        assertEquals(1, aa.getAndAdd(i, 2));
        assertEquals(3, aa.get(i));
        assertEquals(3, aa.getAndAdd(i, -4));
        assertEquals(-1, aa.get(i));
    }
}
 
Example 15
Source File: AtomicDoubleArray.java    From JSAT with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new AtomicDoubleArray of the given length, with all values 
 * initialized to zero
 * @param length the length of the array
 */
public AtomicDoubleArray(int length)
{
    larray = new AtomicLongArray(length);
    long ZERO = Double.doubleToRawLongBits(0.0);
    for(int i = 0; i < length; i++)
        larray.set(i, ZERO);
}
 
Example 16
Source File: Atomic8Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * AtomicLongArray updateAndGet updates with supplied function and
 * returns result.
 */
public void testLongArrayUpdateAndGet() {
    AtomicLongArray a = new AtomicLongArray(1);
    a.set(0, 1);
    assertEquals(18L, a.updateAndGet(0, Atomic8Test::addLong17));
    assertEquals(35L, a.updateAndGet(0, Atomic8Test::addLong17));
    assertEquals(35L, a.get(0));
}
 
Example 17
Source File: ByteArrayOrdinalMap.java    From zeno with Apache License 2.0 5 votes vote down vote up
/**
 * Create an AtomicLongArray of the specified size, each value in the array will be EMPTY_BUCKET_VALUE
 */
private AtomicLongArray emptyKeyArray(int size) {
    AtomicLongArray arr = new AtomicLongArray(size);
    for(int i=0;i<arr.length();i++) {
        arr.set(i, EMPTY_BUCKET_VALUE);
    }
    return arr;
}
 
Example 18
Source File: LongStatsDeltaAggregator.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private void initializeArray(AtomicLongArray arr){
  for(int i = 0; i<arr.length() ; i++){
    arr.set(i, Long.valueOf(0));
  }
}
 
Example 19
Source File: ThreadSafeBitSet.java    From hollow with Apache License 2.0 4 votes vote down vote up
/**
 * Return a new bit set which contains all bits which are contained in *any* of the specified bit sets.
 *
 * @param bitSets the other bit sets
 * @return the resulting bit set
 */
public static ThreadSafeBitSet orAll(ThreadSafeBitSet... bitSets) {
    if(bitSets.length == 0)
        return new ThreadSafeBitSet();

    int log2SegmentSize = bitSets[0].log2SegmentSize;
    int numLongsPerSegment = bitSets[0].numLongsPerSegment;

    ThreadSafeBitSetSegments segments[] = new ThreadSafeBitSetSegments[bitSets.length];
    int maxNumSegments = 0;

    for(int i=0;i<bitSets.length;i++) {
        if(bitSets[i].log2SegmentSize != log2SegmentSize)
            throw new IllegalArgumentException("Segment sizes must be the same");

        segments[i] = bitSets[i].segments.get();
        if(segments[i].numSegments() > maxNumSegments)
            maxNumSegments = segments[i].numSegments();
    }

    ThreadSafeBitSetSegments newSegments = new ThreadSafeBitSetSegments(maxNumSegments, numLongsPerSegment);

    AtomicLongArray segment[] = new AtomicLongArray[segments.length];

    for(int i=0;i<maxNumSegments;i++) {
        for(int j=0;j<segments.length;j++) {
            segment[j] = i < segments[j].numSegments() ? segments[j].getSegment(i) : null;
        }

        AtomicLongArray newSegment = newSegments.getSegment(i);

        for(int j=0;j<numLongsPerSegment;j++) {
            long value = 0;
            for(int k=0;k<segments.length;k++) {
                if(segment[k] != null)
                    value |= segment[k].get(j);
            }
            newSegment.set(j, value);
        }
    }

    ThreadSafeBitSet or = new ThreadSafeBitSet(log2SegmentSize);
    or.segments.set(newSegments);
    return or;
}
 
Example 20
Source File: HeapChunkSpace.java    From greycat with Apache License 2.0 4 votes vote down vote up
private void capacity_extends() {
    int new_maxEntries = this._maxEntries * 2;
    this._graph.log().warn("extends cache capacity from " + this._maxEntries + " to " + new_maxEntries);
    int new_hashEntries = new_maxEntries * HASH_LOAD_FACTOR;
    Stack new_lru = new HeapFixedStack(new_maxEntries, true);
    Stack new_dirties = new HeapFixedStack(new_maxEntries, false);
    AtomicIntegerArray new_hashNext = new AtomicIntegerArray(new_maxEntries);
    AtomicIntegerArray new_hash = new AtomicIntegerArray(new_hashEntries);
    for (int i = 0; i < new_maxEntries; i++) {
        new_hashNext.set(i, -1);
    }
    for (int i = 0; i < new_hashEntries; i++) {
        new_hash.set(i, -1);
    }
    AtomicReferenceArray<Chunk> new_chunkValues = new AtomicReferenceArray<Chunk>(new_maxEntries);
    AtomicLongArray new_chunkWorlds = new AtomicLongArray(new_maxEntries);
    AtomicLongArray new_chunkTimes = new AtomicLongArray(new_maxEntries);
    AtomicLongArray new_chunkIds = new AtomicLongArray(new_maxEntries);
    HeapAtomicByteArray new_chunkTypes = new HeapAtomicByteArray(new_maxEntries);
    AtomicLongArray new_chunkMarks = new AtomicLongArray(new_maxEntries);
    for (int i = 0; i < new_maxEntries; i++) {
        new_chunkMarks.set(i, 0);
    }

    byte type;
    long world;
    long time;
    long id;
    Chunk chunk;
    long marks;
    int index;

    int offset = (int) _dirtiesStack.dequeueTail();
    while (offset != -1) {
        new_dirties.enqueue(offset);
        offset = (int) _dirtiesStack.dequeueTail();
    }

    for (int i = 0; i < _maxEntries; i++) {
        new_lru.dequeue(i);

        type = _chunkTypes.get(i);
        world = _chunkWorlds.get(i);
        time = _chunkTimes.get(i);
        id = _chunkIds.get(i);
        chunk = _chunkValues.get(i);
        marks = _chunkMarks.get(i);
        new_chunkTypes.set(i, type);
        new_chunkWorlds.set(i, world);
        new_chunkTimes.set(i, time);
        new_chunkIds.set(i, id);
        new_chunkValues.set(i, chunk);
        new_chunkMarks.set(i, marks);

        if (_deep_priority) {
            index = (int) HashHelper.tripleHash(type, world, time, id, new_hashEntries);
        } else {
            index = (int) HashHelper.simpleTripleHash(type, world, time, id, new_hashEntries);
        }

        int previous_hash = new_hash.get(index);
        new_hash.set(index, i);
        new_hashNext.set(i, previous_hash);

        if (marks == 0) {
            new_lru.enqueue(i);
        }
        if (_dirtiesStack.dequeue(i)) {
            new_dirties.enqueue(i);
        }
    }

    this._maxEntries = new_maxEntries;
    this._hashEntries = new_hashEntries;
    this._lru = new_lru;
    this._dirtiesStack = new_dirties;
    this._hashNext = new_hashNext;
    this._hash = new_hash;
    this._chunkValues = new_chunkValues;
    this._chunkWorlds = new_chunkWorlds;
    this._chunkTimes = new_chunkTimes;
    this._chunkIds = new_chunkIds;
    this._chunkTypes = new_chunkTypes;
    this._chunkMarks = new_chunkMarks;
}