AtomicInteger 婧愮爜鍒嗘瀽

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AtomicInteger 婧愮爜鍒嗘瀽相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/memory' title='memory'>memory   res   tor   tin   cin   閫氳繃   ted   鏁扮粍鍏冪礌   expected   

AtomicInteger

鏀寔鍘熷瓙鏇存柊鐨?int 鍊笺€?/code>

鍒涘缓瀹炰緥

    /**
     * 鍙師瀛愭洿鏂扮殑 int 鍊?     */
    private volatile int value;

    /**
     * 鍒涘缓鍒濆鍊间负 initialValue 鐨勬柊 AtomicInteger 瀹炰緥
     */
    public AtomicInteger(int initialValue) {
        value = initialValue;
    }

    /**
     * 鍒涘缓鍒濆鍊间负 0 鐨勬柊 AtomicInteger 瀹炰緥
     */
    public AtomicInteger() {
    }

/** * 璇诲彇鍊? * with memory effects as specified by {@link VarHandle#getVolatile}. */ public final int get() { return value; }

/** * 浠ュ師瀛愭柟寮忓皢褰撳墠鍊煎姞 1锛屽苟杩斿洖鏃у€笺€? * with memory effects as specified by {@link VarHandle#getAndAdd}. */ public final int getAndIncrement() { return U.getAndAddInt(this, AtomicInteger.VALUE, 1); } Unsafe# /** * 1锛夊師瀛愬湴灏嗙粰瀹氱殑鍊肩疮鍔犲埌褰撳墠鍊笺€佹垨鎸囧畾绱㈠紩涓?offset 鐨勬暟缁勫厓绱犱笂銆? */ @HotSpotIntrinsicCandidate public int getAndAddInt(Object o, long offset, int delta) { int v; do { // 浠?volatile 鐨勬柟寮忚鍙栧€? v = getIntVolatile(o, offset); } while (!weakCompareAndSetInt(o, offset, v, v + delta)); // 杩斿洖鏃у€? return v; } @HotSpotIntrinsicCandidate public boolean weakCompareAndSetInt(Object o, long offset, int expected, int x) { return compareAndSetInt(o, offset, expected, x); } /** * 濡傛灉褰撳墠鍊兼槸 expected锛屽垯灏嗙洰鏍囧€兼洿鏂颁负 x锛岃鎿嶄綔鍏锋湁 volatile 璇诲拰鍐欏唴瀛樿涔夈€? * <p>This operation has memory semantics of a {@code volatile} read * and write. Corresponds to C11 atomic_compare_exchange_strong. */ @HotSpotIntrinsicCandidate public native boolean compareAndSetInt(Object o, long offset, int expected, int x);

/** * 浠ュ師瀛愭柟寮忓啓鍏ユ柊鍊硷紝骞惰繑鍥炴棫鍊? * with memory effects as specified by {@link VarHandle#getAndSet}. */ public final int getAndSet(int newValue) { return U.getAndSetInt(this, AtomicInteger.VALUE, newValue); }

/** * 浠ュ師瀛愭柟寮忓皢褰撳墠鍊煎噺 1锛屽苟杩斿洖鏃у€? * with memory effects as specified by {@link VarHandle#getAndAdd}. */ public final int getAndDecrement() { return U.getAndAddInt(this, AtomicInteger.VALUE, -1); }

/** * 浠ュ師瀛愭柟寮忓皢缁欏畾鍊间笌褰撳墠鍊肩浉鍔狅紝骞惰繑鍥炴棫鍊? * with memory effects as specified by {@link VarHandle#getAndAdd}. */ public final int getAndAdd(int delta) { return U.getAndAddInt(this, AtomicInteger.VALUE, delta); }

/** * 鍘熷瓙鏇存柊褰撳墠鍊间负鍑芥暟寮忔帴鍙?updateFunction 鐨勮绠楀€硷紝骞惰繑鍥炴棫鍊笺€? */ public final int getAndUpdate(IntUnaryOperator updateFunction) { // 璇诲彇鏃у€? int prev = get(), next = 0; for (boolean haveNext = false;;) { if (!haveNext) { // 璁$畻鏂板€? next = updateFunction.applyAsInt(prev); } // 鍘熷瓙鏇存柊鍊硷紝濡傛灉鎴愬姛鍒欒繑鍥炴棫鍊? if (weakCompareAndSetVolatile(prev, next)) { return prev; } // 鏇存柊澶辫触鍒欓噸鏂拌鍙栨棫鍊硷紝濡傛灉鍑虹幇 ABA 闂锛屽垯涓嶄細閲嶆柊璁$畻 haveNext = prev == (prev = get()); } }

/** * 鍘熷瓙鏇存柊褰撳墠鍊间负鍑芥暟寮忔帴鍙?accumulatorFunction 鐨勮绠楀€硷紝骞惰繑鍥炴棫鍊? */ public final int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction) { // 璇诲彇鏃у€? int prev = get(), next = 0; for (boolean haveNext = false;;) { if (!haveNext) { // 鍩轰簬鏃у€煎拰 x 璁$畻鏂板€? next = accumulatorFunction.applyAsInt(prev, x); } // 鍘熷瓙鏇存柊鏃у€? if (weakCompareAndSetVolatile(prev, next)) { return prev; } haveNext = prev == (prev = get()); } }

/** * 浠ュ師瀛愭柟寮忓皢褰撳墠鍊煎姞 1锛屽苟杩斿洖鏂板€? * with memory effects as specified by {@link VarHandle#getAndAdd}. */ public final int incrementAndGet() { return U.getAndAddInt(this, AtomicInteger.VALUE, 1) + 1; }

/** * 浠ュ師瀛愭柟寮忓皢褰撳墠鍊煎噺 1锛屽苟杩斿洖鏂板€? * with memory effects as specified by {@link VarHandle#getAndAdd}. */ public final int decrementAndGet() { return U.getAndAddInt(this, AtomicInteger.VALUE, -1) - 1; }

/** * 浠ュ師瀛愭柟寮忓皢缁欏畾鍊间笌褰撳墠鍊肩浉鍔狅紝骞惰繑鍥炴柊鍊? * with memory effects as specified by {@link VarHandle#getAndAdd}. */ public final int addAndGet(int delta) { return U.getAndAddInt(this, AtomicInteger.VALUE, delta) + delta; }

/** * 浠ュ師瀛愭柟寮忔洿鏂板€笺€愭柊鍊奸€氳繃鍑芥暟寮忔帴鍙h绠楀緱鍒帮紝鍙傛暟涓烘棫鍊笺€戯紝骞惰繑鍥炴柊鍊笺€? */ public final int updateAndGet(IntUnaryOperator updateFunction) { int prev = get(), next = 0; for (boolean haveNext = false;;) { if (!haveNext) { next = updateFunction.applyAsInt(prev); } if (weakCompareAndSetVolatile(prev, next)) { return next; } haveNext = prev == (prev = get()); } }

/** * 浠ュ師瀛愭柟寮忔洿鏂板€笺€愭柊鍊奸€氳繃鍑芥暟寮忔帴鍙h绠楀緱鍒帮紝鍙傛暟涓烘棫鍊煎拰鍙傝€冩洿鏂板€笺€戯紝骞惰繑鍥炴柊鍊? */ public final int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction) { int prev = get(), next = 0; for (boolean haveNext = false;;) { if (!haveNext) { next = accumulatorFunction.applyAsInt(prev, x); } if (weakCompareAndSetVolatile(prev, next)) { return next; } haveNext = prev == (prev = get()); } }

以上是关于AtomicInteger 婧愮爜鍒嗘瀽的主要内容,如果未能解决你的问题,请参考以下文章

JDK婧愮爜鍒嗘瀽鍒濇鏁寸悊

Memcached婧愮爜鍒嗘瀽涔媔tems.c

log4j2婧愮爜鍒嗘瀽-1

婧愮爜鍒嗘瀽涔?--HashMap鏌ユ壘銆佸垹闄ゃ€侀亶鍘?/h1>

绮惧敖 MyBatis 婧愮爜鍒嗘瀽 - SqlSession 浼氳瘽涓?SQL 鎵ц鍏ュ彛

鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽