nlighter-language="java" class="EnlighterJSRAW">private AtomicInteger counter = new AtomicInteger(3);

public void addCounter() {
    for (;;) {
        int oldValue = counter.get();
        int newValue = oldValue + 1;
        if (counter.compareAndSet(oldValue, newValue))
            return;
    }
}</pre>
<p> <a href="/articles/9703.html#more-9703" class="more-link">&#8230;</p>
<p class="read-more"><a class="btn btn-default" href="/articles/9703.html"> 阅读全文<span class="screen-reader-text"