Java Code Examples for java.util.concurrent.atomic.AtomicInteger#getAndDecrement()

The following examples show how to use java.util.concurrent.atomic.AtomicInteger#getAndDecrement() . 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: LinkedBlockingQueue.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public E poll() {
    final AtomicInteger count = this.count;
    if (count.get() == 0)
        return null;
    E x = null;
    int c = -1;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        if (count.get() > 0) {
            x = dequeue();
            c = count.getAndDecrement();
            if (c > 1)
                notEmpty.signal();
        }
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 2
Source File: LinkedBlockingQueue.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public E take() throws InterruptedException {
    E x;
    int c = -1;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        while (count.get() == 0) {
            notEmpty.await();
        }
        x = dequeue();
        c = count.getAndDecrement();
        if (c > 1)
            notEmpty.signal();
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 3
Source File: LinkedBlockingQueue.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public E take() throws InterruptedException {
    E x;
    int c = -1;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        while (count.get() == 0) {
            notEmpty.await();
        }
        x = dequeue();
        c = count.getAndDecrement();
        if (c > 1)
            notEmpty.signal();
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 4
Source File: ForceableLinkedBlockingQueue.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public E take() throws InterruptedException {
    E x;
    int c = -1;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        while (count.get() == 0) {
            notEmpty.await();
        }
        x = dequeue();
        c = count.getAndDecrement();
        if (c > 1)
            notEmpty.signal();
    } finally {
        takeLock.unlock();
    }
    if (c <= capacity)  // GEMFIRE changed == to <=
        signalNotFull();
    return x;
}
 
Example 5
Source File: PriorityBlockingQueue.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
public E poll() {
    final AtomicInteger count = this.count;
    if (count.get() == 0) return null;
    E x = null;
    int c = -1;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        if (count.get() > 0) {
            x = opQueue(null);
            c = count.getAndDecrement();
            if (c > 1) notEmpty.signal();
        }
    } finally {
        takeLock.unlock();
    }
    if (c == capacity) signalNotFull();
    return x;
}
 
Example 6
Source File: ForceableLinkedBlockingQueue.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public E take() throws InterruptedException {
    E x;
    int c = -1;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        while (count.get() == 0) {
            notEmpty.await();
        }
        x = dequeue();
        c = count.getAndDecrement();
        if (c > 1)
            notEmpty.signal();
    } finally {
        takeLock.unlock();
    }
    if (c <= capacity)  // GEMFIRE changed == to <=
        signalNotFull();
    return x;
}
 
Example 7
Source File: AtomicIntegerDemo.java    From rome with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    int temValue = 0;
    AtomicInteger value = new AtomicInteger(0);
    temValue = value.getAndSet(3);
    // 首先get,获取到当前value的值为0,并赋值给temValue,之后设置新值3,此时value为3
    System.out.println("temValue = " + temValue + " value = " + value);

    temValue = value.getAndIncrement();
    System.out.println("temValue = " + temValue + " value = " + value);

    temValue = value.getAndDecrement();
    System.out.println("temValue = " + temValue + " value = " + value);

    temValue = value.getAndAdd(10);
    System.out.println("temValue = " + temValue + " value = " + value);
}
 
Example 8
Source File: PriorityObjectBlockingQueue.java    From android-open-project-demo with Apache License 2.0 6 votes vote down vote up
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
    E x = null;
    int c = -1;
    long nanos = unit.toNanos(timeout);
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        while (count.get() == 0) {
            if (nanos <= 0)
                return null;
            nanos = notEmpty.awaitNanos(nanos);
        }
        x = opQueue(null);
        c = count.getAndDecrement();
        if (c > 1)
            notEmpty.signal();
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 9
Source File: LinkedBlockingQueue.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public E take() throws InterruptedException {
    E x;
    int c = -1;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        while (count.get() == 0) {
            notEmpty.await();
        }
        x = dequeue();
        c = count.getAndDecrement();
        if (c > 1)
            notEmpty.signal();
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 10
Source File: PriorityPendingQueue.java    From floodlight_with_topoguard with Apache License 2.0 6 votes vote down vote up
public E take() throws InterruptedException {
    E x;
    int c = -1;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        try {
            while (count.get() == 0)
                notEmpty.await();
        } catch (InterruptedException ie) {
            notEmpty.signal(); // propagate to a non-interrupted thread
            throw ie;
        }
        x = extract();
        c = count.getAndDecrement();
        if (c > 1)
           notEmpty.signal();
        } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 11
Source File: LinkedBlockingQueue.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public E take() throws InterruptedException {
    E x;
    int c = -1;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        while (count.get() == 0) {
            notEmpty.await();
        }
        x = dequeue();
        c = count.getAndDecrement();
        if (c > 1)
            notEmpty.signal();
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 12
Source File: LinkedBlockingQueue.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public E take() throws InterruptedException {
    E x;
    int c = -1;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        while (count.get() == 0) {
            notEmpty.await();
        }
        x = dequeue();
        c = count.getAndDecrement();
        if (c > 1)
            notEmpty.signal();
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 13
Source File: LinkedBlockingQueue.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public E poll() {
    final AtomicInteger count = this.count;
    if (count.get() == 0)
        return null;
    E x = null;
    int c = -1;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        if (count.get() > 0) {
            x = dequeue();
            c = count.getAndDecrement();
            if (c > 1)
                notEmpty.signal();
        }
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 14
Source File: LinkedBlockingQueue.java    From jdk-1.7-annotated with Apache License 2.0 6 votes vote down vote up
public E poll() {
    final AtomicInteger count = this.count;
    if (count.get() == 0)
        return null;
    E x = null;
    int c = -1;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        if (count.get() > 0) {
            x = dequeue();
            c = count.getAndDecrement();
            if (c > 1)
                notEmpty.signal();
        }
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 15
Source File: AlertBlockQueue.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
public E poll() {
    final AtomicInteger count = this.count;
    if (count.get() == 0)
        return null;
    E x = null;
    int c = -1;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        if (count.get() > 0) {
            x = dequeue();
            c = count.getAndDecrement();
            if (c > 1)
                notEmpty.signal();
        }
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 16
Source File: KafkaRollerTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testControllerNotInitiallyRollable(VertxTestContext testContext) {
    PodOperator podOps = mockPodOps(podId -> succeededFuture());
    StatefulSet sts = buildStatefulSet();
    AtomicInteger count = new AtomicInteger(2);
    TestingKafkaRoller kafkaRoller = new TestingKafkaRoller(sts, null, null, podOps,
        null, null, null,
        brokerId -> {
            if (brokerId == 2) {
                boolean b = count.getAndDecrement() == 0;
                log.info("Can broker {} be rolled now ? {}", brokerId, b);
                return succeededFuture(b);
            } else {
                return succeededFuture(true);
            }
        },
        2);
    doSuccessfulRollingRestart(testContext, kafkaRoller,
            asList(0, 1, 2, 3, 4),
            asList(0, 1, 3, 4, 2));
}
 
Example 17
Source File: LinkedBlockingQueue.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public E poll() {
    final AtomicInteger count = this.count;
    if (count.get() == 0)
        return null;
    E x = null;
    int c = -1;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        if (count.get() > 0) {
            x = dequeue();
            c = count.getAndDecrement();
            if (c > 1)
                notEmpty.signal();
        }
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 18
Source File: VerifyClassLinkage.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void verify(String clazz, byte[] data, Map<String,Boolean> loadable, ClassLoader loader, AtomicInteger maxWarn)
        throws IOException, BuildException {
    //log("Verifying linkage of " + clazz.replace('/', '.'), Project.MSG_DEBUG);
    Set<String> dependencies = dependencies(data);
    //System.err.println(clazz + " -> " + dependencies);
    for (String clazz2 : dependencies) {
        Boolean exists = loadable.get(clazz2);
        if (exists == null) {
            exists = loader.getResource(clazz2.replace('.', '/') + ".class") != null;
            loadable.put(clazz2, exists);
        }
        if (!exists) {
            String message = clazz + " cannot access " + clazz2;
            if (failOnError) {
                throw new BuildException(message, getLocation());
            } else if (maxWarn.getAndDecrement() > 0) {
                log("Warning: " + message, Project.MSG_WARN);
            } else {
                log("(additional warnings not reported)", Project.MSG_WARN);
                return;
            }
        } else {
            //log("Working reference to " + clazz2, Project.MSG_DEBUG);
        }
    }
}
 
Example 19
Source File: LinkedBlockingQueue.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
public E take() throws InterruptedException {
    E x;
    int c = -1;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        while (count.get() == 0) {
            notEmpty.await();
        }
        x = dequeue();
        c = count.getAndDecrement();
        if (c > 1)
            notEmpty.signal();
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}
 
Example 20
Source File: AbstractMessageTaskTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Before
public void setupTest() {
  peersDoTimeout = new AtomicBoolean(false);
  peerCountToTimeout = new AtomicInteger(0);
  ethPeers = spy(new EthPeers(EthProtocol.NAME, TestClock.fixed(), metricsSystem));
  final EthMessages ethMessages = new EthMessages();
  final EthScheduler ethScheduler =
      new DeterministicEthScheduler(
          () -> peerCountToTimeout.getAndDecrement() > 0 || peersDoTimeout.get());
  ethContext = new EthContext(ethPeers, ethMessages, ethScheduler);
  final SyncState syncState = new SyncState(blockchain, ethContext.getEthPeers());
  transactionPool =
      TransactionPoolFactory.createTransactionPool(
          protocolSchedule,
          protocolContext,
          ethContext,
          TestClock.fixed(),
          metricsSystem,
          syncState,
          Wei.of(1),
          TransactionPoolConfiguration.builder().build(),
          true,
          Optional.empty());
  ethProtocolManager =
      EthProtocolManagerTestUtil.create(
          blockchain,
          ethScheduler,
          protocolContext.getWorldStateArchive(),
          transactionPool,
          EthProtocolConfiguration.defaultConfig(),
          ethPeers,
          ethMessages,
          ethContext);
}