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

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

Introduction

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

Prototype

public final int getAndSet(int newValue) 

Source Link

Document

Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle#getAndSet .

Usage

From source file:org.apache.activemq.web.RestTest.java

@Test(timeout = 15 * 1000)
public void testProperties() throws Exception {
    int port = getPort();

    HttpClient httpClient = new HttpClient();
    httpClient.start();//from   w w  w. jav  a  2s  . c om

    final CountDownLatch latch = new CountDownLatch(1);
    final StringBuffer buf = new StringBuffer();
    final AtomicInteger status = new AtomicInteger();
    httpClient.newRequest("http://localhost:" + port + "/message/testPost?type=queue&property=value")
            .method(HttpMethod.POST).send(new BufferingResponseListener() {
                @Override
                public void onComplete(Result result) {
                    status.getAndSet(result.getResponse().getStatus());
                    buf.append(getContentAsString());
                    latch.countDown();
                }
            });

    latch.await();
    assertTrue("success status", HttpStatus.isSuccess(status.get()));

    final CountDownLatch latch2 = new CountDownLatch(1);
    final StringBuffer buf2 = new StringBuffer();
    final AtomicInteger status2 = new AtomicInteger();
    final HttpFields responseFields = new HttpFields();
    httpClient.newRequest("http://localhost:" + port + "/message/testPost?readTimeout=1000&type=Queue")
            .method(HttpMethod.GET).send(new BufferingResponseListener() {
                @Override
                public void onComplete(Result result) {
                    responseFields.add(result.getResponse().getHeaders());
                    status2.getAndSet(result.getResponse().getStatus());
                    buf2.append(getContentAsString());
                    latch2.countDown();
                }
            });

    latch2.await();
    assertTrue("success status", HttpStatus.isSuccess(status2.get()));

    HttpFields fields = responseFields;
    assertNotNull("Headers Exist", fields);
    assertEquals("header value", "value", fields.getStringField("property"));
}

From source file:org.apache.activemq.web.RestTest.java

@Test(timeout = 15 * 1000)
public void testAuth() throws Exception {
    int port = getPort();

    HttpClient httpClient = new HttpClient();
    httpClient.start();/*from ww w  . j a v a2 s. com*/

    final CountDownLatch latch = new CountDownLatch(1);
    final StringBuffer buf = new StringBuffer();
    final AtomicInteger status = new AtomicInteger();
    httpClient.newRequest("http://localhost:" + port + "/message/testPost?type=queue")
            .header("Authorization", "Basic YWRtaW46YWRtaW4=").method(HttpMethod.POST)
            .send(new BufferingResponseListener() {
                @Override
                public void onComplete(Result result) {
                    status.getAndSet(result.getResponse().getStatus());
                    buf.append(getContentAsString());
                    latch.countDown();
                }
            });

    latch.await();
    assertTrue("success status", HttpStatus.isSuccess(status.get()));
}

From source file:org.apache.nifi.controller.FlowController.java

/**
 * Updates the number of threads that can be simultaneously used for
 * executing processors.//  www .j  ava 2 s.  c om
 *
 * @param maxThreadCount This method must be called while holding the write
 * lock!
 */
private void setMaxThreadCount(final int maxThreadCount, final FlowEngine engine,
        final AtomicInteger maxThreads) {
    if (maxThreadCount < 1) {
        throw new IllegalArgumentException();
    }

    maxThreads.getAndSet(maxThreadCount);
    if (null != engine && engine.getCorePoolSize() < maxThreadCount) {
        engine.setCorePoolSize(maxThreads.intValue());
    }
}

From source file:org.spout.api.chat.ChatArguments.java

public ChatArguments append(final ChatSection section) {
    final AtomicInteger previousIndex = new AtomicInteger();
    int i;//from  ww w  .  ja v  a2 s.  co  m
    for (Map.Entry<Integer, List<ChatStyle>> entry : section.getActiveStyles().entrySet()) {
        i = entry.getKey();
        if (entry.getKey() != -1) {
            append(section.getPlainString().substring(previousIndex.getAndSet(i), i));
        }
        append(entry.getValue());
    }
    if (previousIndex.get() < section.length()) {
        append(section.getPlainString().substring(previousIndex.get(), section.getPlainString().length()));
    }
    return this;
}

From source file:org.wso2.andes.kernel.MessageCountFlusher.java

/**
 * Update the store with new message counts
 *
 * @param queueName  count is updated in this queue
 * @param difference count is updated by this value in store
 *///from   w  ww  .j a va  2 s. c  o m
private void flushToStore(String queueName, AtomicInteger difference) {
    int count = 0;
    try {
        // Get the current count and make decisions from that value.
        // If not, within the execution of method some other thread might update the value and subsequent reads
        // of the value will be different leading to unpredictable behaviour.
        // We get the current count and reset the value at the same time so that this call is going to
        // take care of the current count only. Any update happen during the method execution will be reflected
        // in subsequent calls to this method.
        count = difference.getAndSet(0);

        if (count > 0) {
            if (log.isDebugEnabled()) {
                log.debug("Increment store count by " + count + " for queue " + queueName);
            }
            contextStore.incrementMessageCountForQueue(queueName, count);
        } else if (count < 0) {
            if (log.isDebugEnabled()) {
                log.debug("Decrement store count by " + count + " for queue " + queueName);
            }
            contextStore.incrementMessageCountForQueue(queueName, count);
        }
    } catch (AndesException e) {
        // On error add back the count. Since the operation didn't run correctly. Next call to this method might
        // get the chance to update the value properly.
        difference.addAndGet(count);
        log.error("Error while updating message counts for queue " + queueName, e);
    }
}