Java進階特性系列--Concurrent

來源:互聯網
上載者:User

標籤:code   port   reads   blog   子類   color   進階   int   jvm   

轉載自 http://www.cnblogs.com/skywang12345/p/java_threads_category.html

JUC:java.util.concurrent

一,JUC原子類

根據修改的資料類型,可以將JUC包中的原子操作類可以分為4類。

1. 基本類型: AtomicInteger, AtomicLong, AtomicBoolean ;
2. 數群組類型: AtomicIntegerArray, AtomicLongArray, AtomicReferenceArray ;
3. 參考型別: AtomicReference, AtomicStampedRerence, AtomicMarkableReference ;
4. 對象的屬性修改類型: AtomicIntegerFieldUpdater, AtomicLongFieldUpdater, AtomicReferenceFieldUpdater 。

這些類存在的目的是對相應的資料進行原子操作。所謂原子操作,是指操作過程不會被中斷,保證資料操作是以原子方式進行的。

 

AtomicLong:

AtomicLong是作用是對長整形進行原子操作。
在32位作業系統中,64位的long 和 double 變數由於會被JVM當作兩個分離的32位來進行操作,所以不具有原子性。而使用AtomicLong能讓long的操作保持原子型。

樣本:

package com.util.concurrent.atomic;import java.util.concurrent.atomic.AtomicLong;public class AtomicLongTest {    public static void main(String[] args) {        // TODO Auto-generated method stub        AtomicLong mAtoLong = new AtomicLong();        mAtoLong.set(0x0123456789ABCDEFL);        System.out.println("toString()" + mAtoLong.toString());        System.out.println("get()" + mAtoLong.get());        System.out.println("intValue()" + mAtoLong.intValue());        System.out.println("longValue()" + mAtoLong.longValue());        System.out.println("incrementAndGet()" + mAtoLong.incrementAndGet());        System.out.println("getAndIncrement()" + mAtoLong.getAndIncrement());        System.out.println("decrementAndGet()" + mAtoLong.decrementAndGet());        System.out.println("getAndDecrement()" + mAtoLong.getAndDecrement());        System.out.println("get()" + mAtoLong.get());        System.out.println("addAndGet()" + mAtoLong.addAndGet(0x10));        System.out.println("getAndAdd()" + mAtoLong.getAndAdd(0x10));        System.out.println("getAndSet()" + mAtoLong.getAndSet(0x0123456789ABCDEEL));        System.out.println("compareAndSet()" + mAtoLong.compareAndSet(0x0123456789ABCDEEL, 0x0123456789ABCDEDL));        System.out.println("get()" + mAtoLong.get());    }}

結果:

toString()81985529216486895get()81985529216486895intValue()-1985229329longValue()81985529216486895incrementAndGet()81985529216486896getAndIncrement()81985529216486896decrementAndGet()81985529216486896getAndDecrement()81985529216486896get()81985529216486895addAndGet()81985529216486911getAndAdd()81985529216486911getAndSet()81985529216486927compareAndSet()trueget()81985529216486893

 

Java進階特性系列--Concurrent

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.