Java AtomicInteger setBitIfUnsetByIndex(final AtomicInteger ai, final int n)

Here you can find the source of setBitIfUnsetByIndex(final AtomicInteger ai, final int n)

Description

set Bit If Unset By Index

License

Open Source License

Parameter

Parameter Description
ai the AtomicInteger to operate on
n the nth bit from the least significant bit to compare

Return

true if operation is conducted, false if the bit is already set.

Declaration

public static boolean setBitIfUnsetByIndex(final AtomicInteger ai, final int n) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.atomic.AtomicInteger;

public class Main {
    /**//from   w ww .jav a  2 s  .  com
     * @return true if operation is conducted, false if the bit is already set.
     * @param ai the AtomicInteger to operate on
     * @param n the nth bit from the least significant bit to compare
     * */
    public static boolean setBitIfUnsetByIndex(final AtomicInteger ai, final int n) {
        if (n >= Integer.SIZE) {
            throw new IllegalArgumentException("Out of int bit index boundary (31)");
        }
        int bitInt = 1 << n;
        int oldValue = ai.get();
        int newValue = oldValue | bitInt;
        while (newValue != oldValue && !ai.compareAndSet(oldValue, newValue)) {
            oldValue = ai.get();
            newValue = oldValue | bitInt;
        }
        return newValue != oldValue;
    }
}

Related

  1. resetCounter()
  2. resetCounters()
  3. resetExceptionCount()
  4. setBit(AtomicInteger i, int mask)
  5. setBitByValue(final AtomicInteger ai, final int value)
  6. storeStoreBarrier()
  7. timeToId(long timeInMillis)
  8. tokenize(String string)
  9. uniqueSequenceId()